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 | e | None of the above | Unicode char literals are declared using single quotes, but none of the declarations here use single quotes. The declaration of char b, is also problematic, because it contains more than one char. |
| 2 | c d | 3 4 | Methods declared within an interface are implicitly public. If no access modifier is included in the method declaration; then, the declaration is implicitly public. An attempt to declare the method using a weaker access privilege, private or protected, results in a compile-time error. |
| 3 | d | Prints: ABAbAb | Instances of type String are immutable. In method m1, the toUpperCase method returns a new instance of type String that contains the value AB, and a reference to the new instance is assigned to reference variable s1. The new value, AB, is printed in method m1. In method m2, the toLowerCase method returns a new instance of type String that contains the value ab, but the String instance referenced by s1 remains unchanged. The original value, Ab, 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 Ab. |
| 4 | e | Compile-time error |
The
|
| 5 | g | Prints: true,true,false | LinkedHashSet does not implement the List interface. LinkedHashSet extends HashSet and implements Collection, Set, Cloneable and Serializable. |
| 6 | b | 2 | An array variable is declared by placing brackets after the identifier or after the type name. A compile-time error occurs at the line marked 2, because the brackets appearing before the identifier for array variable a4 are not associated with the type or the identifier. |
| 7 | a | 1 | Please note that this question asks which line does NOT result in a compile-time error. The modifier, synchronized, is a method modifier, but is not a class modifier. Any attempt to declare a synchronized class results in a compile-time error. Since the synchronized modifier specifies an implementation detail it makes no sense to use it with an abstract method that provides no implementation. |
| 8 | d | Prints: 0,1,1 | The try block contains two statements. The first invokes method m1, and the subsequent statement contains a post increment expression with the variable, a, as the operand. Method m1 throws a WhiteException exception, so variable a is not incremented as control passes to the catch block where b is incremented. Although Color.m1 declares a ColorException in the throws clause, a subclass of Color is free to declare only a subclass of ColorException in the throws clause of the overriding method. |
| 9 | f | None of the above | 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. Transient and volatile are not method modifiers. |
| 10 | d | Prints: 1212 | The reference variable s3 is initialized with a reference to an instance of type String containing the value "12". The expression s1 += s2 is equivalent to the expression s1 = s1 + s2. Further simplification produces s1 = "1" + "2" = "12". The expression s3 += s1 is equivalent to the expression s3 = "12" + "12" = "1212". |
| 11 | d | Prints: double,double,double |
The
|
| 12 | h | Prints: true,true,true | LinkedHashSet is a subclass of HashSet; therefore, it is also a subclass of AbstractSet, and it implements the Collection interface. |
| 13 | a e | -ea -enableassertions | Two command-line switches used to enable assertions in non-system classes are -ea and -enableassertions. |
| 14 | d | char d = -1; | The assignment of -1 to char d generates a compile-time error, because the primitive char type is unsigned. A negative int can not be assigned to a char without an explicit cast. If the literal value -1 were cast to type char then the result would be \uffff. |
| 15 | 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().
At line 1, 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.
At line 2,
a new instance of the named inner class
B
is created using the
class instance creation expression
new A.B().
The fully qualified name of class
B
is
A.B.
Since the class instance creation expression
new A.B()
appears within a method that is a member of class
A,
the use of the fully qualified name is unnecessary.
Within method
|
| 16 | a |
|
The
|
| 17 | f | HashMap | 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. The requirement to allow null elements is not satisfied by a Hashtable. TreeMap and TreeSet store elements in a sorted order based on the key. The iteration order of LinkedHashMap and LinkedHashSet is not unspecified. By default, the iteration order of LinkedHashMap and LinkedHashSet is based on the order in which elements were inserted. Optionally, the iteration order of the LinkedHashMap can be set to the order in which the elements were last accessed. |
| 18 | b d e | If assertions are not enabled at run time it prints nothing. With assertions enabled it prints nothing. The assert statement is being used to check a class invariant--something that must be true about each instance of the class. | This question is an example of using assertions to check a class invariant--something that must be true about each instance of the class. Although a class invariant must be true before and after the execution of each public method, the invariant is typically only checked at the end of each method and constructor. |
| 19 | f g | Compile-time error at line 3 Compile-time error at line 4 | 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 qualified class instance creation expression this.new B() generates a compile-time error, because the keyword this can not be used within a static method. Since methods m1, m2 and m3 are static methods, an instance of the named inner class B can not be created inside any of the three methods using an unqualified class instance creation expression of the form new ClassType(ArgumentListopt). |
| 20 | e | None of the above | A dead thread can not be restarted. |
| 21 | h | Hashtable | The requirement to store key/value pairs is directly satisfied by a Hashtable or any concrete implementation of the Map interface. The List and Set interfaces recognize objects, but do not recognize keys and values. The requirement to NOT allow null elements is satisfied by Hashtable, but not by HashMap or any of the other Collection implementations that were introduced with Java 1.2 and later. |
| 22 | f | Compile-time error | A throw statement is the first statement in the outer try block. A throw statement appearing in a try block causes control to pass out of the block. Consequently, statements can not be reached if they appear in the block after the throw statement. The switch statement that appears after the throw statement is unreachable and results in a compile-time error. |
| 23 | b | Prints: 012 | This trick question has a while-loop nested inside of a do-loop. For each iteration, the values of i, j and k are as follows: (1,0,0)(2,0,1)(3,0,2). |
| 24 | b | Prints: B,SuperB,A,SuperA | The expression A.this.s1 is an example of a qualified this expression. It accesses the variable s1 declared in class A. The expression A.super.s1 is equivalent to ((SuperA)A.this).s1. It accesses the variable s1 declared in class SuperA. |
| 25 | d | Prints: 012345 | This trick question has a do-loop nested inside of a while-loop. For each iteration, the values of i and j are as follows: (1,0)(1,1)(1,2)(1,3)(2,4)(3,5). |
| 26 | b | Prints: FFT | The expression used to assign variable b1 is equivalent to the expression used to assign variable b2. The results demonstrate that the conditional operator (?:) groups from right-to-left. |
| 27 | b | Prints: DDDD | The instance method that is invoked depends on the run-time type of the object--not the compile-time type of the reference. In each case, the method m1 is invoked on an object of type D; so the implementation of m1 in type D is selected each time. |
| 28 | g | None of the above | Prints 31,0. The expression (-1 & 0x1f) is equal to (0xffffffff & 0x1f), and both are equal to the hex value 0x1f or decimal 31. The expression (8 << -1) is equivalent to (8 << 0xffffffff). If the left hand operand of a shift expression is of type int, then the right hand operand is implicitly masked with the value 0x1f. In other words, the expression (8 << -1) is equivalent to (8 << (-1 & 0x1f)). By replacing -1 with the hexadecimal representation we have (8 << (0xffffffff & 0x1f)). By evaluating the right hand operand we have (8 << 31). When 8 is shifted 31 bits to the left, the result is zero since the only non-zero bit is lost as it is shifted beyond the most significant bit of the int data type. |