Exam Prep Practice Questions
Question 1
What will be the result of calling the following method with an input of 2?
1. public int adder( int N ){ 2. return 0x100 + N++ ; 3. }
-
The method will return 258.
-
The method will return 102.
-
The method will return 259.
-
The method will return 103.
Answer A is correct. The hexadecimal constant 0x100 is 256 in decimal so adding 2 results in 258. The post increment of N will have no effect on the returned value. The method would return 102 if the literal constant were decimal, but it is not. Therefore, answer B is incorrect. Answers C and D represent incorrect arithmetic.
Question 2
What happens when you attempt to compile and run the following code?
1. public class Logic { 2. static int minusOne = -1 ; 3. static public void main(String args[] ){ 4. int N = minusOne >> 31 ; 5. System.out.println("N = " + N ); 6. } 7. }
-
The program will compile and run, producing the output "N = -1".
-
The program will compile and run, producing the output "N = 1".
-
A runtime ArithmeticException will be thrown.
-
The program will compile and run, producing the output "N = 0".
Answer A is correct. The >> operator extends the sign as the shift operation is performed. The program would have compiled and run, producing the output "N = 1" if the >>> operator, which shifts in a zero bit, had been specified, but it was not. Therefore, answer B is incorrect. An ArithmeticException is typically thrown due to integer division by zero, not by a shift operation. Therefore, answer C is incorrect. Answer D does not occur because the >> operator extends the sign as the shift is performed.
Question 3
What would be the result of running the following method with an input of 67?
1. public int MaskOff( int n ){ 2. return n | 3 ; 3. }
-
The method would return 3.
-
The method would return 64.
-
The method would return 67.
-
The method would return 0.
Answer C is correct. The bit pattern of 67 is 1000011, so the bitwise OR with 3 would not change the number. The method would have returned 3 if the bitwise AND operator & had been used, but this is the OR operator. Therefore, answer A is incorrect. The method would have returned 64 if the XOR operator ^ had been used, but it was not. Therefore, answer B is incorrect. Answer D cannot result from the OR of 67 with 3.
Question 4
How many String objects are created in the following code?
1. String A, B, C ; 2. A = new String( "1234" ) ; 3. B = A ; 4. C = A + B ;
-
One
-
Two
-
Three
-
Four
The correct answer is B. Both A and B refer to the same String object, whereas C refers to a String created by concatenating two copies of A. Therefore, only two String objects have been created, and all other answers are incorrect.
Question 5
Which of the following versions of initializing a char variable would cause a compiler error? [Check all correct answers.]
-
char c = - 1 ;
-
char c = '\u00FF' ;
-
char c = (char) 4096 ;
-
char c = 4096L ;
-
char c = 'c' ;
-
char c = "c" ;
Answers A, D, and F are correct. In answer A, the literal creates a negative int that the compiler recognizes as being outside the normal range of char (the only unsigned integer primitive). In answer D, an explicit cast would be required to convert the literal long into a char. In answer F, the string literal could be used only to initialize a String object. The other options are legal assignments to a char primitive. Therefore, answers B, C, and E are incorrect. Note that questions that ask you to identify statements that will not compile are likely to appear on the exam.
Question 6
What happens when you try to compile and run the following code?
1. public class EqualsTest{ 2. public static void main(String args[]){ 3. Long LA = new Long( 7 ) ; 4. Long LB = new Long( 7 ) ; 5. if( LA == LB ) System.out.println("Equal"); 6. else System.out.println("Not Equal"); 7. } 8. }
-
The program compiles but throws a runtime exception in line 5.
-
The program compiles and prints "Equal".
-
The program compiles and prints "Not Equal".
-
The compiler objects to line 5.
Answer C is correct.When used with objects, the == operator tests for identity. Because LA and LB are different objects, the test fails. All other answers are incorrect.
Question 7
What happens when you try to compile and run the following code?
1. public class EqualsTest{ 2. public static void main(String args[]){ 3. char A = '\u0005' ; 4. if( A == 0x0005L ) { 5. System.out.println("Equal"); 6. } 7. else { 8. System.out.println("Not Equal"); 9. } 10. } 11. }
-
The compiler reports "Invalid character in input" in line 3.
-
The program compiles and prints "Not Equal".
-
The program compiles and prints "Equal".
-
The compiler objects to the use of == to compare a char and a long.
Answer C is correct. The compiler promotes variable A to a long before the comparison so answer D does not occur. The compiler does not report "Invalid character in input" in line 3 because this is the correct form for initializing a char primitive. Therefore, answer A is incorrect. Because answer C is correct, answer B cannot possibly be the correct answer.
Question 8
In the following code fragment, you know that the getParameter call may return a null if there is no parameter named size:
1. int sz ; 2. public void init(){ 3. sz = 10 ; 4. String tmp = getParameter("size"); 5. if( tmp != null X tmp.equals("BIG")) sz = 20 ; 6. }
Which logical operator should replace X in line 5 to ensure that a NullPointerException is not generated if tmp is null?
-
&
-
&&
-
|
-
||
The correct answer is B, the "short-circuited" AND operator. All of the other operators would attempt to run the equals method on the tmp variable, even if it were null, causing a NullPointerException. Therefore, answers A, C, and D are incorrect.
Question 9
What would happen if you tried to compile and run the following code?
1. public class EqualsTest{ 2. public static void main(String args[]){ 3. Long L = new Long( 7 ); 4. if( L.equals( 7L )) System.out.println("Equal"); 5. else System.out.println("Not Equal"); 6. } 7. }
-
The program would compile and print "Equal".
-
The program would compile and print "Not Equal".
-
The compiler would object to line 4.
-
A runtime cast error would occur at line 4.
Answer C is correct. The compiler knows that the equals method takes an Object rather than a primitive as input. Because the program does not compile, answers A, B, and D are incorrect.
Question 10
What would happen if you tried to compile and run the following code?
1. public class EqualsTest{ 2. public static void main(String args[]){ 3. Object A = new Long( 7 ); 4. Long L = new Long( 7 ) ; 5. if( A.equals( L )) System.out.println("Equal"); 6. else System.out.println("Not Equal"); 7. } 8. }
-
The program would compile and print "Equal".
-
The program would compile and print "Not Equal".
-
The compiler would object to line 5.
-
A runtime cast error would occur at line 5.
Answer A is correct. The Long object created in line 3 does not lose its identity when cast to Object A, so the equals method knows the class is correct and compares the values. Because answer A is correct, answer B is obviously incorrect. Answers C and D do not occur because this is the correct form for comparing objects with the equals method. Therefore, they are incorrect.