Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1LinkedList  ArrayList and Vector both use an array to store the elements of the list; so access to any element using an index is very fast. A LinkedList is implemented using a doubly linked list; so access to an element requires the list to be traversed using the links.  
2Prints: true,true,false  The List and Set interfaces extend the Collection interface; so both List and Set could be cast to type Collection without generating a ClassCastException. Therefore, the first two of the three relational expressions return true. The Map interface does not extend Collection. The reference variable c1 refers to an instance of TreeMap; so the relational expression c1 instanceof Collection returns the value false.  
3true   
4HashSet  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. Adding and removing an element in a TreeSet involves walking the tree to determine the location of the element. A HashSet stores the elements in a hashtable; so elements in a HashSet can be accessed almost as quickly as elements in an array as long as the hash function disperses the elements properly. Although the LinkedHashSet is not among the answer options it could arguably satisfy the requirements. However, the put and remove methods of the LinkedHashSet are a little slower than the same methods of the HashSet due to the need to maintain the linked list through the elements of the LinkedHashSet.  
5Prints: false,true,true  The Map interface does not extend Collection. The reference variable a1 refers to an instance of HashMap; so the relational expression a1 instanceof Collection returns the value false. The List and Set interfaces extend the Collection interface; so both List and Set could be cast to type Collection without generating a ClassCastException. Therefore, the second and third relational expressions return true.  
6true  If two objects are equal according to the equals method, then the hashcodes produced by the hashCode method must also be equal. If two objects are not equal according to the equals method, then the hashcodes may or may not be equal. It is preferable that unequal objects have different hashcodes, but that is not always possible. Since the hash code value is a 32 bit primitive int, it is not possible to produce a unique hash code for each value of a primitive long.  
7Map  The List and Set interfaces do not support key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries.  
8Prints: true,false  HashSet implements the Set interface, but not the SortedSet interface.  
9false  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. It is preferable that unequal objects have different hashcodes, but that is not always possible. Since the hash code value is a 32 bit primitive int, it is not possible to produce a unique hash code for each value of a primitive long.  
10LinkedList  ArrayList and Vector both use an array to store the elements of the list. When an element is inserted into the middle of the list the elements that follow the insertion point must be shifted to make room for the new element. The LinkedList is implemented using a doubly linked list; an insertion requires only the updating of the links at the point of insertion. Therefore, the LinkedList allows for fast insertions and deletions.  
11Prints: true,true  TreeSet implements the Set interface and the SortedSet interface.  
12Prints: 1026  The hash code is calculated using all three array elements. Since the object is immutable, the hash code is only calculated the first time the hashCode method is invoked.  
13TreeMap  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.  
14Prints: true,true,true  The 1.2 version of Java introduced the updated Vector class that implements the List interface.  
15a  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.  
16Set  The Map interface organizes entries as key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries.  
17Prints: true,false,false  HashSet is a subclass of AbstractSet and AbstractCollection; therefore, it implements the Collection interface. HashMap and Hashtable do not implement the Collection interface. Instead, HashMap extends AbstractMap and implements the Map interface. Hashtable extends Dictionary and implements the Map interface.  
18None of the above  Suppose that the hashCode method is invoked on the same object more than once during the same execution of a Java application. If no information used in the equals comparison is modified, then each invocation of the hashCode method must produce the same hash code value. The hashCode methods of classes B and C will always return the same value during an execution of the Java application and are therefore consistent with the hash code contract. Even so, the hashCode methods of classes B and C are not efficient, because they will cause hashtables to place every instance of classes B and C in the same bucket. The hashCode method of class D is appropriate and will allow a hash table to operate efficiently.  

Copyright © 2002-2003, Dan Chisholm
All rights reserved.