cs
Which of the following is equivalent to !(a && (c < d))
!a || (c >= d)
Which of the following operators is used to obtain the remainder when dividing?
%
int Sum, A, B; Sum = 120; A = 45; B = 20; if ((Sum *B < 1000) <insert code> (A > B)) { System.out.println ("Hello"); } else { System.out.println ("Goodbye"); } Under what condition can we replace the <insert code> statement and cause a short circuit?
&&
Which of the following is a character literal?
'A'
Which of the following is equivalent to the boolean expression: !(a || b) && !(b > c)
(!a && !b) && b <= c
Which of the following will short- circuit?
(20 > 80) && (80 > 30)
Which of the following will short-circuit?
(30 < 40) || (40 > 50)
Which of the following will short- circuit?
(30 > 40) && (40 > 50)
The Boolean expression !((A < B) || (C > D)) is equivalent to which of the following expressions?
(A >= B) && (C <=D)
The Boolean expression !((A < B) && (C > D)) is equivalent to which of the following expressions?
(A >= B) || (C <= D)
Which is the increment operator?
++
Which is the decrement operator?
--
Consider the following code segment. String s1 = "A"; String s2 = "H"; System.out.print(s1.compareTo("B")); System.out.print(" "); System.out.println(s2.compareTo("B")); What is printed as a result of executing the code segment?
-1 6
Given the following: for (int i = 0; i < 2; i++) { for (int j = 1; j <= 3; j+= 2) { System.out.print (i * j + " " ); } System.out.println (); } What is the output?
0 0 1 3
Conider the following code segment. String s1 = "Computer Science"; String s2 = "Computer"; String s3 = "Science"; System.out.print(s1.indexOf(s2)); System.out.println(" "); System.out.println(s1.indexOf(s3)); What will be the output when the code segment executes?
0 9
Given String name = "Emelia"; What is the index of the m?
1
Given the following code segment: int result; int i = 8; int j = 5; result = i/j; What is the value of result?
1
Given the following: int a = 9; int b = 5; while (a % b != 0) { int temp = a; a = b; b = temp % b; } System.out.println (b); What is the output?
1
Given the following: for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { System.out.print (i + " " + j ); } System.out.println (); } What is the output?
1 1 1 2 1 3 2 1 2 2 2 3
Consider the following code segment. int q = 0; for (int p = 0; p < 10; p++) { q++; p += q; System.out.println(p + " "); } What is printed as a result of executing the code segment?
1 4 8 13
Consider the following code segment. int q = 0; for (int p = 0; p < 5; p++) { q = q + p; } System.out.println(q); What is printed as a result of executing the code segment?
10
What value is stored in result if int result = 13 - 3 * 6 / 4 % 3;
12
Consider the following code segment. int x = 0; for (int y = 1; y <= 12; y ++) { while (x <= y) { if (x % 2 == 0) x += 2; else y -= 2; } } System.out.println(x); What is printed as a result of executing the code segment?
14
Consider the following code segment. int n = 6; int count = 0; for (int p = 1; p <= n-1; p++) for(int q = 0; q < p; q++) count++; System.out.println(count); What is the value of count when the code segment finishes executing?
15
What is the value of x after the following code executes? int x = 2; int y = -1; while (x < 15) { x = (int) Math.pow(x, Math.abs(y * 2)); }
16
Convert the following hexadecimal number to decimal. A8
168
Convert 271oct to decimal form.
185
Convert the following binary number to decimal: 1100 0101
197
How many different values can a boolean variable hold?
2
What would be the value of dresult if double dresult = 5 / 2;
2
Given the following: for (int i = 2; i < 10; i += 2) System.out.print ( i + " " ); What is the output?
2 4 6 8
Consider the following code segment. int x = <some integer greater than zero> int n = 0; if(x < 500) { if(x > 750) n = 100; else n = 200; } else { if (x < 300) n = 300; else n = 200; } System.out.println(n); What is printed as a result of executing the code segment?
200
Consider the following code segment. int count = 0; for (int p = 0; p <= 5; p++) { for (int q = p; q <= 5; q++) { count++; } } System.out.println(count); What is printed as a result of executing the code segment?
21
Convert the hexadecimal number 18 to its decimal equivalent.
24
How many different numbers can be represented with 8 bits?
256
What is the value of num after the following? num = Math.ceil (2.2);
3
Consider the following code segment. for (int k = 2; k < 15; k++) { if (k % 2 == 1) System.out.print(k + " "); } What is printed as a result of executing the code segment?
3 5 7 9 11 13
Consider the following code segment. int x = <some integer greater than zero> int n = 100; if(x < 1000) { if (x > 1000) n = 200; else n = 300; } else { if (x < 1000) n = 400; else n = 300; } System.out.println(n); What is printed as a result of executing the code segment?
300
Consider the following code segment: for (int i = 1; i < 5; i++){ for (int j = 8; j >= 0; j--){ System.out.println("!"); } } How many times will a '!' be printed?
36
Convert 100101bin to decimal form.
37
Consider the following: int i = 4; while (i <= 4) { System.out.print(i + " "); i++; } What is the output?
4
Given the following: int x = 0; int n = 2; for (int a = 0; a < n; a++) { for (int b = 0; b < n; b++) { x++; } } System.out.println (x); What is the output?
4
Consider the following code segment. String s1 = "Queen Mary"; String s2 = "1936 Queen Mary"; String s3 = "Queen Elizabeth"; System.out.print(s2.indexOf(s1) + " "); System.out.println(s3.indexOf(s1)); What will be output when the code segment executes?
5 -1
What is the output of the following if A = 5 and B = 6? if (A != B) { System.out.println (A + " is greater than "+ B); } else { if (A > B) { System.out.println (A + " is greater than " + B); } else { System.out.println (A + " is less than " + B); } }
5 is greater than 6
What is the output of the following if A = 5 and B = 6? if (A == B) { System.out.println (A + " does not equal " + B); } else { if (A > B) { System.out.println (A + " is less than " + B); } else { System.out.println (A + " is greater than " + B); } }
5 is greater than 6
What would be the value of dresult if double dresult = 11.0 / (-3 + 5);
5.5
What value is stored in result if int result = 93 / 2 + (45 % 10)
51
Consider the following code segment. int count = 5; for (int p = 0; p < 7; p ++) { if (p % 3 == 0) count --; else count ++; } System.out.println(count); What is printed as a result of executing the code segment?
6
Consider the following code segment. int p = 0; while (p < 5) { int q = 0; while (q < 4) { p += q; q++; System.out.println(p + " " + q); } } What is the last output when the segment executes?
6 4
Consider the following code segment. int k = 0; for (k = 1; k <= 5; k++) { k++; } k++; System.out.println(k); What is printed as a result of executing the code segment?
8
Convert 3C4hex to decimal form.
964
What is the value of A, B and C after the following code segment? int A = 45; int B = 10; int C = A % B; if (C != 0) { A = B; B = C; }
A = 10; B = 5; C = 5;
Consider Java's two overloaded substring methods. String substring (int from, int to) String substring(int from) Given a string s, when will s.substring(p, q) return the same String value as s.substring(p)?
Anytime, as long as p == s.length()
Consider the following method. public static void conditionTest(int num1, int num2) { if ((num1>0) && (num2>0)) { if (num1>num2) System.out.println("A"); else System.out.println("B"); } else if ((num2<0) && (num1<0)) { System.out.println("C"); } else if (num2 < 0) { System.out.println("D"); } else { System.out.println("E"); } } What is the output from conditionTest(3,-2)?
D
Which Boolean law is demonstrated by !(A && B) is equivalent to !A || !B
DeMorgan's Law
#one is a proper identifier.
False
/** Precondition: s != null * n < s.length () */ public String method09a(int n, String s) { return s.substring(n); } /** Precondition: s != null * n < s.length () */ public String method 09b(int n, String s) { return s.substring(n, s.length()); } For which values of s is the same value returned by both methods?
For all string values of s
int Sum, A, B; Sum = 120; A = 45; B = 20; if (!((B + A) < Sum) || ((B * A) < Sum)) { System.out.println ("Good Day!!"); } else { if (!(Sum == 130) &&((A + B) == 35)) { System.out.println ("Bad Day!!"); } else { System.out.println ("Good Day and Bad Day!!"); } }
Good Day and Bad Day!!
int Sum, A, B; Sum = 120; A = 45; B = 20; if (!((B + A) >= Sum) || ((B * A) < Sum)) { System.out.println ("Good Day!!"); } else { if (!(Sum == 130) &&((A + B) == 35)) { System.out.println ("Bad Day!!"); } else { System.out.println ("Good Day and Bad Day!!"); } }
Good Day!!
int Sum, A, B; Sum = 120; A = 45; B = 20; if ((Sum *B < 1000) && (A > B)) { System.out.println ("Hello"); } else { System.out.println ("Goodbye"); } prints:
Goodbye
What will s1 equal after the following code executes? String s1 = "Hi There"; String s4 = s1; String s5 = "Bye"; String s2 = s1.toLowerCase(); String s3 = s1.toUpperCase(); s1 = s3; s4 = null;
HI THERE
int Sum, A, B; Sum = 120; A = 45; B = 20; if ((Sum *B < 1000) || (A > B)) { System.out.println ("Hello"); } else { System.out.println ("Goodbye"); } prints:
Hello
What would be the output of the following? System.out.print("Hello"); System.out.print("Hello");
HelloHello
Consider the following code segment, where m is a variable of the type int: if (m > 0) { if (1000/m) % 2 == 0) System.out.println("even"); else System.out.println("odd"); } else System.out.println("not positive"); Which of the following code segments are equivalent to the one above (that is, produce the same output as the one above regardless of the value of x)? I. if(m <= 0) System.out.println("not positive"); else if ((1000/m) % 2 == 0) System.out.println("even"); else System.out.println("odd"); II. if(m > 0 && (1000/m) % 2 == 0) System.out.println("even"); else if (m <= 0) System.out.println("not positive"); else System.out.println("odd"); III. if( (1000/m) % 2 == 0) { if (m <= 0) System.out.println("not positive"); else System.out.println("even"); } else { if (m <= 0) System.out.println("not positive"); else System.out.println("odd"); }
I and II
Given the following code: String s1 = new String("hi"); String s2 = new String("hi there"); String s3 = s2.substring(0,2); String s4 = new String("hi"); Which of the following would return true? I. s1.equals(s3) II. s1 == s4 III.s1.equals(s4)
I and III only
At a certain high school students receive letter grades based on the following scale. Numeric Score Letter Grade 90 or Above A From 80 to 89 inclusive B From 70 to 79 inclusive C Below 70 F Which of the following code segments will assign the correct string to grade for a given integer score? I. if (score >= 90) grade = "A"; else if (score >= 80) grade = "B"; if (score >= 70) grade = "C"; else grade = "F"; II. if (score >= 90) grade = "A"; if (score >= 80 && score <= 89) grade = "B"; if (score >= 70 && score <= 79) grade = "C"; if (score < 70) grade = "F"; III. if (score >= 90) grade = "A"; else if (score >= 80) grade = "B"; else if (score >= 70) grade = "C"; else grade = "F";
II and III only
What does it mean for Java to be case-sensitive?
Java interprets lowercase and uppercase letters differently.
int Sum, A, B; Sum = 120; A = 45; B = 20; if (Sum >= 120) { System.out.println ("Julia Roberts"); } else { if ((A > 0) && (B < 0)) { System.out.println (" is a famous "); } else { System.out.println ("Star");} } prints:
Julia Roberts
int Sum, A, B; Sum = 120; A = 45; B = 20; if (Sum >= 120) { System.out.println ("Julia Roberts"); if ((A > 0) || (B < 0)) { System.out.println (" is a famous "); } else { System.out.println ("Star");} } prints:
Julia Roberts is a famous
What class do we use to get input from the user?
Scanner
int Sum, A, B; Sum = 120; A = 45; B = 20; if (Sum > 120) { System.out.println ("Julia Roberts"); } else { if ((A > 0) && (B < 0)) { System.out.println (" is a famous "); } else { System.out.println ("Star");} } prints:
Star
Consider the following code segment: int x = 0, y = 3; String op = "/"; if (op.equals("/") && (x != 0) && (y/x > 2)) { System.out.println("OK"); } else { System.out.println("Failed"); } Which of the following statements about this code is true?
The code will compile and execute without error; the output will be Failed.
Given the following: for (int i = 0; i < 2; i++) { for (int j = 1; j <= 3; j+= 2) { System.out.print (i * j + " " ); } System.out.println (); }
The inner most loop
Given the following: for (int i = 0; i < 2; i++) { for (int j = 1; j <= 3; j+= 2) { System.out.print (i * j + " " ); } System.out.println (); } Which for loop iterates more times?
The inner most loop
What would be the output of the following? System.out.println ("The sum is " + (12 + 13));
The sum is 25
Integer data can be stored in a real number data type. That is the we can store and int into a double.
True
String name = "Emelia"; What is the value of name.equalsIgnoreCase("emelia"); ?
True
firstPlace is a proper identifier.
True
how_much is a proper identifier.
True
length is a proper identifier.
True
seven11 is a proper identifier.
True
int q = <some integer value greater than 0 > int p = <some integer value greater than q > while (p > q) { if (p % 2 == 0) p--; else q++; } System.out.println(q + " " + p); What kind of values are printed when the segment executes?
Two positive integers, such that p equals q
How do we refer to the fact that Strings can not ever change?
We say that they are immutable.
Consider the following code segment: int p = 3; int q = 2; int sum = 0; while (p <= 5) { sum += p % q; p++; q--; } What is the value of sum after the code is executed?
You get a run-time error
What are the values of a and b after the following loop finishes? int a = 8, b=3, temp = 0; for (int i = 1; i <= 6; i++) { temp = a; a = i + b; b = temp - i; }
a = 11, b = 0
Consider the following method: public double confuse(double a) { a = a * a; a = a * a; a = a * a; return a; } Which of the following could replace the first three lines of the body of confuse so that an equivalent result is returned?
a = Math.pow(a, 8);
String a = "Furman"; String b = a; String c = "Furman"; Which of the following will evaluate to FALSE?
a == c
Which of the following is equivalent to the boolean expression: !(a <= b || a == c)
a > b && a != c
What are the final values of a, b, and c? int a = 4, b = 9, c = -3; if (a >= b) if (c < b) a = b + c; else c = a - c; else if (c < a) b = c - b;
a= 4, b= -12, c= -3
What are the final values of a, b, and c? int a = 4, b = -9, c = 7; if (a > b && b > c) if (b < a) b = -b; else a = -a; else if (a > b || b > c) if (a < c) c = -c; else if (! (c > b)) b = -a;
a= 4, b= -9, c= -7
When is the following Boolean expression true (a and b are integers)? (a < b) || !(b > a)
always true
In an if statement the condition is called the
boolean expression
For a boolean expression containing the logical AND operator to be true, _____________ expressions it joins must be true.
both
Which of the following will declare variable initial of type char?
char initial;
The body of a program is enclosed in ________________.
curly braces - { }
Which of the following will create a variable to hold a persons height and assign it to the value 59.5 inches?
double height = 59.5;
For a boolean expression containing the logical OR operator to be true, _____________ expressions it joins must be true.
either of the
7up is a proper identifier
false
Given: int a = 50; int b = 30; boolean ans; ans = ((a % b) > 20); What is the value of ans?
false
The Boolean expression (A || B) && (!A && !B) evaluates to
false in all cases
What keyword is used when creating a constant?
final
Consider the following: int i = 4; while (i <= 4) { System.out.print(i + " "); i++; } Which for loop is equivalent of to above while loop?
for (int i = 4; i <= 4; i++) { System.out.print (i + " "); }
Given the following: for (int i = 2; i < 10; i += 2) System.out.print ( i + " " ); What is the step expression / increment?
i += 2;
Given the following: for (int i = 2; i < 10; i += 2) System.out.print ( i + " " ); Which of the following is the boolean expression?
i < 10
Given: int i; Which is a proper way to initialize i to a value?
i = 7;
Which of the following is equivalent to: i++;
i = i + 1;
We want the user to be able to enter "yes" in any case they want, ie yes, Yes, YES, etc. Which of the following will evaluate to true? String userinput = input.next(); i. userinput.equals ("yes"); ii. userinput.equalsIgnoreCase ("yes"); iii. userinput = userinput.toLowerCase (); userinput.equals ("yes");
ii and iii
A while loop is consider to be a ________________ loop.
indefinite
Consider the following: int i = 4; while (i >= 4) { System.out.print(i + " "); i++; } What is the output?
infinite
Which of the following will create a variable named cars that is assigned to the number of cars in stock at a dealership? The number of cars currently on the lot is 56. Use the best data type.
int cars = 56;
Given the following: for (int i = 2; i < 10; i += 2) System.out.print ( i + " " ); Which of the following is the intializing expression?
int i = 2
Which of the following will properly declare a variable months that holds the number of the current month?
int months;
Which of the following is NOT an example of a primitive data type?
integer
A nested if
is when there is an if statement in the body of another if statement.
Which of the following is equivalent to: j *=7;
j = j * 7;
Consider the following code segment. String s1 = "Djakarta"; String s2 = s1.substring(3, 7); String s3 = s1.substring(3); System.out.println(s2); System.out.println(s3); What will be output when the code segment executes?
kart karta
Which of the following will assign the length of the String name to len?
len = name.length();
Which of the following is a proper identifier?
myCalculation
Consider the following: int i = 4; while (i < 4) { System.out.print(i + " "); i++; } What is the output?
nothing
Which of the following will generate a random number between 1 and 10?
num = (int)(Math.random() *10) + 1;
Which of the following will compute the absolute value of -8 and assign it to num?
num = Math.abs(-8);
Which of the following will assign num to the square of the quantity a plus b?
num = Math.pow (a + b, 2);
Which of the following is equivalent to: num--;
num = num - 1;
Which of the following is the proper way to create a class header for a class named Triangle?
public class Triangle
Which of the following is the header that is used for the main method?
public static void main (String [] args)
What will the method String substring (int from, int to), which belongs to the String class, return?
returns a String beginning at from and ending at (to - 1)
Which of the following will assign sRoot to the square root of 36?
sRoot = Math.sqrt(36);
In Java, statements end in a ____________________.
semicolon
Which of the following is equivalent to: sum = sum + 5;
sum += 5;
What would be the output of the following? System.out.println ("The sum is " + 24 + 25);
the sum is 2425
What method can we use to lowercase all the letters in a String?
toLowerCase()
What method could we use to make all the letters in the String upper case?
toUpperCase()
What can be stored in a boolean variable?
true and false
The Boolean expression (A || B) || !(A || B) evaluates to
true in all cases.
The Boolean expression !(A || B) evaluates to
true whenever both A is false and B is false.
The Boolean expression (A || B) || A is
true whenever either A is true or B is true
The Boolean expression (A || B) && (!A || !B) evaluates to
true whenever only A is true or only B is true.
The Boolean expression (A || B) && A is true
whenever A is true
The Boolean expression (A && B) || B is true
whenever B is true
The boolean expression (A || B ) && B is true
whenever B is true
The Boolean expression (A && B) || (A && B) is true
whenever both A is true and B is true
The boolean expression (A && B) && A is true
whenever both A is true and B is true