Você está na página 1de 3

Collections 2 Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
1 d Prints: false,true,true LinkedHashMap does not implement the Collection interface.
2 g Prints: true,true,false LinkedHashSet does not implement the List interface.
3 h Prints: true,true,true
The Vector.elements method returns an Enumeration over the elements of
the Vector. The Vector class implements the List interface and extends
4 e Prints: true,false,false
AbstractList; so it is also possible to obtain an Iterator over a Vector by
invoking the iterator or listIterator method.
5 c Prints: false,true,false A ListIterator can be obtained by invoking the listIterator method.
6 d Prints: false,true,true ListIterator extends Iterator.
The HashMap, HashSet and Hashtable classes are all implemented with an
internal hashtable that organizes the elements in buckets according to the
hashcode and not according to the order of insertion. The LinkedHashMap
and LinkedHashSet classes also use an internal hashtable, and both also
f maintain a linked list through all of the elements. The order of the list is
7 LinkedHashMap LinkedHashSet
g determined by the order of insertion. Optionally, the LinkedHashMap can
maintain the order of the list based on the time of the most recent access of
each element. The TreeMap and TreeSet classes are both implemented using
a tree structure that is ordered based on a Comparator or the Comparable
interface.
The Enumeration interface was introduced with Java 1.0 to provide an easy
means of moving through the elements of a Vector or the keys or values of a
The Enumeration interface declares Hashtable. The Iterator interface was introduced with the collections
only two methods: framework with Java 1.2. The Iterator interface declares three methods:
b
8 hasMoreElements and hasNext, next and remove. The first two methods, hasNext and next, are
d
nextElement. The Iterator interface similar to the two methods declared in the Enumeration interface,
declares a total of three methods. hasMoreElements and nextElement. The third method of the Iterator
interface, remove, provides new functionality relative to the Enumeration
interface.
The Iterator interface declares three methods: hasNext, next and remove.
The ListIterator interface was introduced in Java 1.2 along with the Iterator
interface. The ListIterator interface extends the Iterator interface and
9 e None of the above. declares additional methods to provide forward and backward iteration
capabilities, List modification capabilities and the ability to determine the
position of the iterator in the List. The ListIterator interface does not extend
the List interface.
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
10 f HashMap
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.
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
11 h Hashtable
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.
12 a LinkedHashMap HashMap The requirement to store key/value pairs is directly satisfied by a concrete
f Hashtable implementation of the Map interface. The List and Set interfaces recognize

page:1
Collections 2 Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
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,
h
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.
The Collections class is not the same as the Collection interface. The
Collections class contains a variety of methods used to work with
13 b Prints: false,false,true collections. For example, Collections.shuffle is used to randomly shuffle the
elements of a Collection. Similarly, the Arrays class provides utility
methods for working with arrays.
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
14 e LinkedHashSet 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.
The RandomAccess interface is a marker interface; so it does not declare
any methods. Its purpose is to provide information about the RandomAccess
capabilities of a List implementation. Generic list algorithms can check to
15 f Prints: true,false,true see if an instance of a List implements the RandomAccess marker interface.
If not, then the algorithm can avoid operations that require fast random
access. Both Vector and ArrayList implement the RandomAccess interface.
LinkedList does not implement RandomAccess.
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
16 d LinkedHashSet LinkedHashSet, the Collection.addAll method will add the elements of the
existing Set to the new instance. Since the iteration order of the
LinkedHashSet is determined by the order of insertion, the iteration order of
the new LinkedHashSet must be the same as the iteration order of the old
Set.
The iteration order of a Map is the order in which an iterator moves through
the elements of the Map. The iteration order of a LinkedHashMap is
determined by the order in which elements are inserted. When a reference to
an existing Map is passed as an argument to the constructor of
17 f LinkedHashMap LinkedHashMap, the Collection.addAll method will add the elements of the
existing Map to the new instance. Since the iteration order of the
LinkedHashMap is determined by the order of insertion, the iteration order
of the new LinkedHashMap must be the same as the iteration order of the
old Map.
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
18 a LinkedHashMap
elements is not satisfied by a Hashtable. The LinkedHashMap offers the
option to remove the least recently used (LRU) element when a new
element is added. The LinkedHashSet does not offer the LRU option.
19 b LinkedHashMap The LinkedHashMap offers the option to remove the least recently used

page:2
Collections 2 Answers

Answers: Certified Java Programmer Mock Exam


No. Answer Remark
(LRU) element when a new element is added. The removeEldestEntry
method is invoked by the put and putAll methods. If removeEldestEntry
returns true, then the least recently used element is removed. A typical
implementation of the method returns true if the size of the LinkedHashMap
exceeds the desired maximum.
A stack or queue must be implemented using a data structure that stores the
elements based on the order of insertion. Any data structure that is
implemented using a hashtable is not a good choice. The ArrayList and
Vector are both implemented using an internal array. Although an array
allows elements to be easily organized based on the order of insertion, an
20 d LinkedList
array does not allow the list of elements to be easily shifted in memory as
elements are appended to the tail of the list and removed from the head of
the list. The LinkedList is implemented using a doubly linked list that
allows elements to be easily appended to the tail of the list, and removed
from the head of the list.
The LinkedList class provides methods such as addFirst, addLast, getFirst,
21 c LinkedList getLast, removeFirst and removeLast that facilitate the implementation of
stacks and queues.
The RandomAccess interface is a marker interface; so it does not declare
any methods. Its purpose is to provide information about the RandomAccess
capabilities of a List implementation. Generic list algorithms can check to
22 g None of the above. see if an instance of a List implements the RandomAccess marker interface.
If not, then the algorithm can avoid operations that require fast random
access. Both Vector and ArrayList implement the RandomAccess interface.
LinkedList does not implement RandomAccess.
The ListIterator interface extends the Iterator interface and declares
The ListIterator interface extends
additional methods to provide forward and backward iteration capabilities,
23 b both the List and Iterator
List modification capabilities and the ability to determine the position of the
interfaces.
iterator in the List.

page:3

Você também pode gostar