| No. | Answer | Remark | |
|---|---|---|---|
| 1 | e | Compile-time error | The access modifiers public, protected and private, can not be applied to variables declared inside methods. |
| 2 | b d g h | strictfp super goto native | |
| 3 | e | 5 | The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors". |
| 4 | a | Prints: int primitive |
The
|
| 5 | g | Run-time error |
The
|
| 6 | e f g | parseDouble toString(double) valueOf | |
| 7 | c | Prints: false,true,false | The Boolean.valueOf method is overloaded: one version accepts a primitive boolean argument; the other accepts a String. If the String value is the word true, then the new Boolean instance will contain the value true. Both upper and lower case letters are acceptable. If the String contains any word other than true or if the String reference is null, then the new instance will contain the value false. |
| 8 | i | Compile-time error |
The
|
| 9 | b | Prints: p,p,S |
The
|
| 10 | b | TreeMap | The requirement to store key/value pairs is directly satisfied by a concrete implementation of the Map interface. The List and Set interfaces recognize objects, but do not recognize keys and values. TreeMap and TreeSet store elements in a sorted order based on the key, but the TreeSet does not support key/value pairs. |
| 11 | h | Prints: true,true,true | The 1.2 version of Java introduced the updated Vector class that implements the List interface. |
| 12 | a d | (a.hashCode() == b.hashCode()) (!g.equals(h)) | If two objects are equal according to the equals method, then the hashcodes must also be equal. If two objects are not equal according to the equals method, then the hashcodes may or may not be equal. If two objects have the same hash code, then the objects may or may not be equal. If two objects have different hashcodes, then the objects must not be equal. |
| 13 | a b e | A final method can not be overridden. All methods declared in a final class are implicitly final. A machine-code generator can inline the body of a final method. | All methods declared in a final class are implicitly final. It is permissible--but not required--that all such methods be explicitly declared final. All private methods are implicitly final. It is permissible--but not required--that all private methods be explicitly declared final. The body of an inline method is inserted directly into the code at the point where the method is invoked. If the method is invoked at 10 different points in the code, then the body can be copied to all 10 points. Inline code runs very quickly, because it removes the need to do the work associated with a method invocation. If the method is executed repeatedly in a loop, then inlining can improve performance significantly. The machine-code generator has the option to inline a final method. |
| 14 | a | 1 | The modifiers, private and static, can be applied to a nested class, but can not be applied to a class that is not nested. A class that is not nested can have public or package access, but not private. The transient modifier can not be applied to any class; because it is a field modifier. |
| 15 | f | None of the above | The following modifiers can be applied to a member class: abstract, private, protected, public, static and final. |
| 16 | d | Prints: 0,0,1,0,1,1 | The nested catch clause is able to catch a Level2Exception or any subclass of it. The switch statement throws an Exception that can not be caught by the nested catch clause; so the nested finally block is executed as control passes to the second of the two outer catch clauses. The outer finally block is executed as control passes out of the try statement. |
| 17 | a e | abstract public | The modifier, abstract, is applicable to an interface declaration, but its use is strongly discouraged; because every interface is implicitly abstract. An interface can not be final. The modifiers, private and protected, are applicable only to an interface declaration that is a member of a directly enclosing class declaration. If an interface is not a member of a directly enclosing class, or if the interface is a member of a directly enclosing interface; then, the modifiers, private and protected, are not applicable. If an interface is declare public, then the compiler will generate an error if the class is not stored in a file that has the same name as the interface plus the extension .java. |
| 18 | b d | '\u0000' to '\uffff' 0 to 65535 | A char is a 16 bit unsigned value; so none of the char values are negative and the minimum value is zero. The maximum value is 216 - 1. |
| 19 | f | Prints: true,false,true | The positive infinity of type float is promoted to the positive infinity of type double. NaN is not equal to anything including itself. |
| 20 | c | Prints: 195ab | Both operands of the first addition operator are promoted from type char to int, and are evaluated as integral numeric values. The right hand operand of the second addition operator is of type String, so the result of the first addition operator is converted to type String, and is concatenated with the right hand operand. As evaluation of the expression continues from left to right, the remaining operands are also converted to type String. |
| 21 | c | Prints: 61433 | On the first pass through the loop, the value of x is 6, so 5 is subtracted from x. On the second pass, the value of x is 1, so 3 is added to x. On the third pass, the value of x is 4, so 1 is subtracted from x. On the fourth pass, the value of x is 3, so the variable, success, is incremented from zero to one. On the final pass, the value of x is 3 and the variable, success, is incremented to the value, 2. The boolean expression of the do loop is now false, so control passes out of the loop. |
| 22 | a d | With assertions enabled it prints ABC followed by an AssertionError message. With assertions disabled it prints ABCE | If the default label of a switch statement should not be reached under normal operating circumstances, then the default label might be a good candidate for the use of an assert statement. |
| 23 | c | C | Please note that this question asks which objects are NOT eligible for garbage collection when method m2 begins to execute? All three references, i1, i2 and i3, refer to object named C; so C is not eligible for garbage collection when method m2 begins to execute. The objects named A and B have references to each other, but no other objects refer to A and B. The objects A and B form an island of islolated objects and are eligible for garbage collection. |
| 24 | c | Compile-time error | Local variables are not initialized automatically, and must be initialized explicitly before attempting to access the value. The local variable i3 will not be initialized if i1 is less than or equal to zero; so the result is a compile-time error. |
| 25 | a | Prints: 78 ABC*$ | When char variables a and b are converted to String types they are printed as *$. When not converted to String types they are promoted to type int, and are printed as the numeric sum of 0x2a and 0x24. At line 1, the expression, a + b, can be evaluated as follows: 0x2a + 0x24 = 42 + 36 = 78. At line 2, the first operand of the expression, " ABC" + a + b, is of type String. Since one operand of the first addition operator is of type String the other operand must be converted to type String: (" ABC" + "*") + b = " ABC*" + b. The process is repeated for the second addition operation: " ABC*" + b = " ABC*" + "$" = " ABC*$" |
| 26 | b | Prints: 147258369 | The array variable a1 is declared with the initializer, {{1,2,3},{4,5,6},{7,8,9}}. The array access expression, a1[0][1] = a1[first subarray][second element] = 2. If the argument of the print statement had been a1[i][j] then the output would have been 123456789. The tricky feature of this question is the reversal of i and j to produce the deceptive array access expression, a1[j][i]. The output is 147258369. |
| 27 | b | Prints: 3122 | The arguments that appear in the class instance creation expression of an anonymous class are passed to a constructor of the superclass. |
| 28 | c | Prints: 5 | The two statements, int a=1 followed by a += ++a + a++, can be rewritten as the single statement, a=(int)((1)+(++a + a++)). Further evaluation produces a=(int)((1)+(2 + 2)). Generally speaking, a compound assignment expression of the form E1 op= E2 can be rewritten as E1=(T)((E1)op(E2)) where T is the type of E1. |