The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.
Today, you can find my book at amazon.com.
| No. | Answer | Remark | |
|---|---|---|---|
| 1 | f | Prints: fffffffe,ffffffff,7fffffff | If the left-hand operand of a shift operator, <<, >> and >>>, is of type int, then the shift distance is always within the range of 0 to 31, inclusive; and is specified by the least significant 5 bits of the right hand operand. Similarly, if the left-hand operand of a shift operator is of type long, then the shift distance is always within the range of 0 to 63, inclusive; and is specified by the least significant 6 bits of the right hand operand. The left shift operator, <<, shifts each bit of the left operand to the left a distance specified by the shift distance. A number of bits that is equal to the shift distance are shifted out of the left-most bit position and are lost. A number of bits that is equal to the shift distance are shifted in at the right. The signed right shift operator, >>, shifts each bit of the left operand to the right a distance specified by the shift distance. A number of bits that is equal to the shift distance are shifted out of the right-most bit position and are lost. A number of bits that is equal to the shift distance are shifted in at the left. The value of each bit that is shifted in at the left is equal to the value of the sign bit. The signed right shift operator maintains the sign of the left operand. The unsigned right shift operator, >>>, is similar to the signed right shift operator except for the fact that each bit shifted in at the left is zero. |
| 2 | e | None of the above | String literals are declared using double quotes, but all of the declarations here use single quotes. |
| 3 | b | Prints: false,true |
|
| 4 | e | SortedMap | The List and Set interfaces do not support key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries. A Map organizes the entries as key/value pairs. The SortedMap is similar to a Map except that the ordering of the elements is determined by a Comparator or the Comparable interface. |
| 5 | c | Prints: false,true,false | LinkedHashMap does not implement the Collection interface or the List interface. |
| 6 | e | Prints: 2,5,9 | The line marked 1 declares three array variables, a2, a3 and a4, and each references an array with components of type int. The line marked 2 declares an array variable, a1, where each component is initialized with a reference to one of the previously created arrays. The array access expression, a1[0][1], is evaluated as follows: a1[0][1] = a1[first subarray][second component] = 2. |
| 7 | b | 2 | An abstract method has no implementation, and is not useful until an extending class implements the method. Private methods are not inherited and can not be overridden. If an abstract method is declared private, then it can not be implemented in a subclass. Although a method may not be both private and abstract, a nested class can be; because another nested class can extend the abstract class and implement any abstract methods. |
| 8 | e | Prints: 0,0,1,0,1 | The first catch block is able to catch a Level3Exception or any subclass of Level3Exception. The second catch block is able to catch a Level2Exception or any subclass of Level2Exception. The switch statement throws a Level2Exception. The try block completes abruptly as control passes to the second catch block where c is incremented. The finally block is also executed, so f is incremented. |
| 9 | e | 5 | All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. No other modifiers can be applied to a field declaration within an interface. |
| 10 | b | 2 | The reference a1 is set to null. String b1 generates a compile-time error, because String literals must be enclosed by double quotes. String c1 is the word null. String d1 is a single quote followed by the word null followed by another single quote. String literals are covered in section 3.10.5 of the JLS. |
| 11 | a | Prints: main,B.m1 | Suppose a superclass method is not private and is accessible to code in a subclass. If the superclass method is declared static, then any subclass method sharing the same signature must also be declared static. Similarly, if the superclass method is not declared static, then any subclass method sharing the same signature must not be declared static. The rules governing method overloading are different. If a superclass method is declared static, then any subclass method that overloads the superclass method is free to be declared static or non-static. Similarly, if a method is declared non-static, then any overloading method is free to be declared static or non-static. Method B.m1() shares the same signature as the non-static superclass method A.m1(), so B.m1() must also be non-static. The method B.m1(String s) overloads the method name m1, but does not share the same signature with any superclass method; therefore, B.m1(String s) can be declared static even though the other methods of the same name are non-static. |
| 12 | d | Prints: AZA | Instances of type String are immutable. In method m1, the replace method returns a new instance of type String that contains the value Y, but the String instance referenced by s1 remains unchanged. The original value, A, is printed in method m1. In method m2, the replace method returns a new instance of type String that contains the value Z, and a reference to the new instance is assigned to reference variable s1. The new value, Z, is printed in method m2. In the main method, a copy of the reference value contained by the reference variable s1 is passed as an argument to methods m1 and m2. Since String instances are immutable, methods m1 and m2 can not change the original String instance that is declared in the main method. Since references are passed by value, methods m1 and m2 can not change the reference variable declared in the main method. Regardless of anything that happens in methods m1 and m2, the reference variable s1 that is declared in the main method will continue to reference the original String instance that contains the value A. |
| 13 | d | Prints: double |
The
|
| 14 | b d | Compile-time error at 2. Compile-time error at 4. |
The
|
| 15 | d | SortedSet | The Map interface organizes entries as key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries. The SortedSet is similar to a Set except that the ordering of the elements is determined by a Comparator or the Comparable interface. |
| 16 | d | Prints: false,true,true | LinkedHashMap does not implement the Collection interface. LinkedHashMap extends HashMap and implements Map, Cloneable and Serializable. |
| 17 | a | 1 | A compile-time error occurs at the line marked 1, because the array variable declaration can not be used to specify the number of components contained in the array. Instead, the dimension expression should be contained in an array creation expression such as the following, new int[3]. |
| 18 | a | 1 | Please note that this question asks which line does NOT result in a compile-time error. An abstract method has no implementation and is not useful until an extending class implements the method. A final method can not be overridden by a subclass method. An abstract final method can not be implemented and is not legal. An abstract class may contain abstract method declarations and is assumed to be incomplete. A final class can not be extended. The implementation of an abstract final class could not be completed. The declaration of class C results in a compiler error, because a final class may not be listed in the extends clause. |
| 19 | g | Prints: 1,0,0,0,1 | The switch statement does not throw an exception; so the switch completes normally. The subsequent statement increments the variable, a; and the try block completes normally. The finally block is also executed, so f is incremented. |
| 20 | a e | abstract public | All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. An abstract method can not also be declared private, static, final, native or synchronized; so the same restriction applies to methods declared within an interface. |
| 21 | f | Prints: 3,0,3 | Java evaluates operands from left to right while respecting operator precedence. The order of operator precedence starting with the lowest is as follows: |, ^, &. Although complete memorization of the operator precedence chart is not necessary, it is a good idea to memorize the three levels that appear in this question. |
| 22 | a d f h | If assertions are enabled at run time it prints an error message. With assertions disabled it prints 1112. The assert statements are being used to check a precondition--something that must be true when the method is invoked. Method m2 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a public method. | Assertions may be enabled or disabled at run time. Since assertions are not always enabled, they should not be used to validate the parameters of public methods. Parameter checking is typically published in the API specification of a method and must be enforced even when assertions are not enabled. Rather than use an assertion, an appropriate runtime exception should be thrown such as IllegalArgumentException, IndexOutOfBoundsException, or NullPointerException. However, an assertion may be used to validate the parameters of a nonpublic method. |
| 23 | c | Prints: A1B0A0B1A0B2 | Class A is the enclosing class of the inner class B. An instance of class B must be associated with an enclosing instance of class A. In a static context, instantiation of a named inner class requires the use of a qualified class instance creation expression of the form Reference.new Identifier(ArgumentListopt). The reference could be provided by a reference variable of the type of the enclosing class, or it could be provided by another class instance creation expression. If the enclosing class is not an inner class, then the enclosing class could be instantiated with an unqualified class instance creation expression such as the following, new EnclosingClass(). The qualified class instance creation expression new A().new B() first creates a new instance of A, then it creates an instance of B. The new instance of A is the second instance created; so the name is A1. The new instance of B is the first instance created; so the name is B0. In the qualified class instance creation expression this.new B(), the keyword this, denotes a reference to the enclosing instance on which the method m2 has been invoked. A new instance of the enclosing class is not created; so the name of the enclosing instance is A0. The new instance of B is the second instance created; so the name is B1. Since method m3 is an instance method, the inner class B can be instantiated using the unqualified class instance creation expression new B(). The enclosing instance is the instance on which the method m3 has been invoked. It is the same instance that is referenced by the keyword this and the reference variable a1. |
| 24 | b c d f | The |
The
|
| 25 | b c e | If assertions are not enabled at run time it prints nothing. With assertions enabled it prints an error message. The assert statement is being used to check a postcondition--something that must be true when the method completes successfully. | Variable c equals 200 when the assertion is checked. |
| 26 | a | Prints: 0001 | This trick question has a do-loop nested inside of a for-loop. For each iteration, the values of i and j are as follows: (0,0)(0,1)(0,2)(1,3). |
| 27 | f | Prints: 1112 | This trick question has a do-loop nested inside of a for-loop. For each iteration, the values of i and j are as follows: (1,0)(1,1)(1,2)(2,3). |
| 28 | b | Prints: D,C,B,A | A field of a superclass can be inherited by a subclass if the superclass field is not private and not hidden by a field declaration in the subclass and is accessible to code in the subclass. The field D.s1 hides C.s1, and C.s1 hides B.s1, and B.s1 hides A.s1. The keyword this serves as a reference to the object on which a method has been invoked. In the field access expression this.s1 appearing on line 1, the keyword this denotes a reference to the object of type D on which method m1 has been invoked. In the field access expression ((C)this).s1 appearing on line 2, the reference denoted by the keyword this is cast from type D to type C. The field that is accessed at run-time depends on the compile-time type of the reference; so the field access expression ((C)this).s1 refers the the variable s1 declared in class C. |