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 | None of the above | All of the declarations are legal. The first three ( 061, '\61', '\061' ) are declared in octal format. The fourth (0x0031) is declared as a hexadecimal literal. The fifth ('\u0031') is a Unicode escape sequence. |
| 2 | b | Prints: false,true | The reference variable s1 is initialized with a reference to an instance of type String containing the value "ABCDEFG". The reference variable s2 is initialized with a reference to an instance of type String containing the value "EFGHIJ". The expression s3 = s1.substring(4,7) initializes the reference variable s3 with a reference to a unique instance of type String containing the value "EFG". The expression s4 = s2.substring(0,3) initializes the reference variable s4 with a reference to a unique instance of type String containing the value "EFG". The expression s3 == s4 compares two unique reference values and produces the value false even though s3 and s4 reference two String instances that contain the same value, "EFG". The expression s3 + s4 produces a unique instance of type String containing the value "EFGEFG". Similarly, the expression s4 + s3 produces a unique instance of type String containing the value "EFGEFG". The expression (s3 + s4).equals(s4 + s3) compares the contents of two unique instances of type String that contain the value "EFGEFG". The result of the comparison is true. |
| 3 | a g h i | b1 b7 b8 b9 |
The
|
| 4 | f | Compile-time error | A compile-time error is generated, because the second catch clause attempts to catch an exception that is never thrown in the try block. |
| 5 | a e | -da -disableassertions | Two command-line switches used to disable assertions are -da and -disableassertions. Remember that all of the letters are lower case. |
| 6 | c d e | 3 4 5 | Methods declared within an interface are implicitly public even if the modifier, public, is omitted from the declaration. Within the body of a class declaration, an attempt to implement the method using a weaker access privilege, private, protected or package access, results in a compile-time error. |
| 7 | a | boolean b1 = true; | There are two primitive boolean values: true and false. Both must be written with lower case letters. Although the C programming language accepts zero as a boolean value, the Java programming language does not. |
| 8 | d | Prints: "ABCDEF" | The reference variable s1 is initialized with a reference to an instance of type String containing the value "ABCDEFG". The expression s2 = s1.substring(0,3) initializes the reference variable s2 with a reference to a unique instance of type String containing the value "ABC". The expression s3 = s1.substring(4,6) creates a unique instance of type String containing the value "EF". The expression c1 = s1.charAt(3) initializes the primitive variable c1 with the value 'D'. The expression String.valueOf(c1) invokes the static valueOf method with an argument of type primitive char and value 'D'. The valueOf method creates a new instance of type String. The value contained by the new instance is "D". The expression s2.concat(String.valueOf(c1)) invokes the concat method on the instance of type String referenced by the variable s2. The instance referenced by s2 contains the value "ABC". The value contained by the argument is "D". The result of the concatenation operation is a new instance of type String containing the value "ABCD". The expression s2.concat(String.valueOf(c1)).concat(s3) invokes the concat method on the previously created instance containing the value "ABCD". The instance referenced by the argument s3 contains the value "EF". The result of the concatenation operation is a new instance of type String containing the value "ABCDEF". |
| 9 | l | None of the above |
All methods of the
|
| 10 | e | Prints: true,false,false |
The
|
| 11 | a | -disableassertions | Two command-line switches used to disable assertions in non-system classes are -da and -disableassertions. Remember that all of the letters are lower case. |
| 12 | g | Prints: fffffffe,fffffffe | For each of the three shift operators, <<, >> and >>>, the shift distance is specified by the right hand operand. If the left operand is of type int, then the shift distance is always within the range 0 to 31, inclusive; and the following expression is always true: (int1 << shift) == (int1 << (shift & 0x1f)). The hexadecimal representation of decimal 31 is 0x1f and the binary representation is 0001 1111. The hexadecimal representation of decimal 33 is 0x21 and the binary representation is 0010 0001. The expression i1 << (33 & 0x1f) is equivalent to (0xffffffff << (0x21 & 0x1f)). Evaluation of the right hand operand of the shift operator produces (0xffffffff << 1). The final result is 0xfffffffe. Similarly, if the left operand is of type long, then the shift distance is always within the range 0 to 63, inclusive; and the following expression is always true: (long1 << shift) == (long1 << (shift & 0x3f)). |
| 13 | b c | The relationship between a class and its superclass is an example of an "is-a" relationship. The relationship between a class and an object referenced by a field within the class is an example of a "has-a" relationship. | Inheritance is an example of an "is-a" relationship, because the subclass "is-a" specialized type of the superclass. The relationship between a class and an object referenced by a field declared within the class is an example of a "has-a" relationship, because the class "has-a" object. |
| 14 | a d e | abs max min | |
| 15 | c | Prints: false,true,false | Both HashMap and TreeMap are subclasses of type AbstractMap; so both implement the Map interface. Neither implements the Collection interface. The TreeMap implements the SortedMap interface, but HashMap does not. |
| 16 | b | Ready | |
| 17 | a f h | LinkedHashMap HashMap Hashtable | 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 Hashtable, HashMap and LinkedHashMap classes store elements in a hashtable. Elements are accessed using a hashcode that identifies the bucket that contains the element. Access time is therefore not dependent on the number of buckets. As long as the hashcode methods of the elements are properly implemented, the time required to access an element in a hashtable remains constant as the number of buckets in the hashtable grows. In contrast, the TreeMap and TreeSet classes store elements in a sorted order in a tree structure. Access to any element requires walking the tree; so access time depends on the size of the tree. |
| 18 | b | Prints: false,false,true |
The
Collections
class is not the same as the
Collection
interface. The
Collections
class contains a variety of methods used to work with
collections. For example,
|
| 19 | b d e h | The |
The
|
| 20 | e | LinkedHashSet | The elements of a Map are key/value pairs; so a Map is not a good choice. A List generally accepts duplicate elements. A Set stores a collection of unique elements. Any attempt to store a duplicate element in a Set is rejected. TreeSet stores elements in a sorted order based on the key. HashSet does not sort the elements based on the key. The iteration order of LinkedHashMap and LinkedHashSet is clearly defined. By default, the iteration order of LinkedHashMap and LinkedHashSet is based on the order in which elements were inserted. While a LinkedHashSet rejects duplicate entries, the LinkedHashMap allows duplicate entries to replace old entries. |
| 21 | b | 2 | 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. Methods declared within an interface are implicitly public even if the modifier, public, is omitted from the declaration. Within the body of a class declaration, an attempt to implement the method using a weaker access privilege, private, protected or package access, results in a compile-time error. An abstract class that implements an interface is free to override any of the inherited method declarations with another abstract method declaration. |
| 22 | a c f |
| |
| 23 | e | Prints: AB C D EF | The trim method creates a new String instance with the leading and trailing white space removed. |
| 24 | d | LinkedHashSet |
The iteration order of a
Set
is the order in which an iterator moves through the elements of the
Set.
The iteration order of a
LinkedHashSet
is determined by the order in which elements are inserted.
When a reference to an existing
Set
is passed as an argument to the constructor of
LinkedHashSet,
the
|
| 25 | c | Prints: i1k1i2j1i3j2 | A while loop is nested inside of a do loop. The while loop iterates once during the first iteration of the do loop. The body of the while loop does not execute during the final iteration of the do loop. |
| 26 | c | Prints: Blue,Blue | 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 | d | Compile-time error at 2 | The method invocation expression a1.m2() generates a compile-time error, because the named method, m2, is declared in class B, but the reference is of the superclass type, A. The reference a1 is of type A; so a1 is able to access only those methods that are declared in class A and subclass methods that override those of class A. Only one method, m1, is declared in A; so a reference of type A can be used to invoke A.m1 or an overriding implementation of m1 that is declared in a subclass of A. Class B extends A and overrides method m1. A reference of type A can be used to invoke method m1 on an instance of type B. Class B declares an additional method, m2, that does not override a method of class A; so a reference of type A can not invoke B.m2. |
| 28 | b | Prints: A0B0J1C1B1J2C2B2 | The initialization statement, labeled A, is processed first. The boolean expression, labeled B, is processed before each iteration of the loop. The body of the loop is processed next followed by the update expression, labeled C. |