| No. | Answer | Remark | |
|---|---|---|---|
| 1 | c | TreeSet | The elements are not key/value pairs; so a Map is not a good choice. A List generally accepts duplicate elements. A Set stores a collection of unique objects; so any attempt to store a duplicate object is rejected. TreeSet stores elements in an order that is determined either by a Comparator or by the Comparable interface. |
| 2 | d | Prints: false,true,true | HashSet implements the Set interface, but not the Map interface. HashMap extends AbstractMap and implements the Map interface. Hashtable extends Dictionary and implements the Map interface. |
| 3 | a b c d | java.lang.Byte java.lang.Integer java.util.Vector java.lang.String | The wrapper classes and the collection classes override the equals and hashCode methods. The String class overrides the equals and hashCode methods, but StringBuffer does not. |
| 4 | d | 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. |
| 5 | h | Prints: true,true,true | All three implement Cloneable. |
| 6 | a b | return 31; return getI1(); | A hashCode method that returns the constant value 31 is consistent with the hash code contract. Even so, a hashCode method that returns the same value regardless of the internal state of the object is not very good, because it will cause hashtables to place every instance of the class in the same bucket. If the equals method determines that two instances are equal, then the two instances must produce the same hash code. For that reason, the hash code must not be calculated using fields that are not used to determine equality. In this case, the equals method determines equality based on the value of i1. The value of i2 is not used to determine equality; therefore, i2 can not be used to calculate the hash code. |
| 7 | a | List | The Map interface organizes entries as key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries. A List allows entries to be accessed using an index. |
| 8 | g | Prints: true,true,false | HashMap does not implement the Collection interface. |
| 9 | e | return 31 * getI1() + getI2(); | All of the statements would produce a hashCode method that is consistent with the hash code contract. The expression 31 * getI1() + getI2() produces the most efficient hashCode method, because it is most likely to produce unique hashcodes for various combinations of i1 and i2. The expression getI1() + getI2() is less efficient, because it produces the same hash code when the values of i1 and i2 are swapped. |
| 10 | c d e f | TreeMap TreeSet HashMap HashSet | The Vector and Hashtable methods are synchronized and do not allow for simultaneous access by multiple threads. The concrete subclasses of the AbstractList, AbstractMap, and AbstractSet classes allow for unsynchronized read operations by multiple threads. Additionally, the sychronized wrapper methods of the Collections class allow for the instantiation of a Collection, List, Map, Set, SortedMap, or SortedSet with synchronized methods. If simultaneous read and write operations are necessary then a synchronized instance should be used. |
| 11 | 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. |
| 12 | c | Prints: false,true,false | LinkedHashMap does not implement the Collection interface or the List interface. |
| 13 | 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. |
| 14 | d | Prints: false,true,true | LinkedHashMap does not implement the Collection interface. |
| 15 | g | Prints: true,true,false | LinkedHashSet does not implement the List interface. |
| 16 | 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,
|
| 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 | 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. |