Chapter SEVEN
Collections


Exam Objectives

Create and use ArrayList, TreeSet, TreeMap, and ArrayDeque objects.

Answers

1. The correct answer is C.
push() inserts the element at the front of the deque. After pushing 1, 2, 3 the queue looks like [3, 2, 1].
poll() retrieves and removes the first element of this deque, 3 in this case.


2. The correct answer is A.
TreeSet doesn't allow null values because when you add an object, if no Comparator is passed to the constructor of the TreeSet (like in this case), this class assumes that the object implements Comparable and tries to call the compareTo() method.


3. The correct answer is B.
The remove() method has two versions, one that takes the index of the element to remove, and another that takes the object to remove.

Since we are passing an int, the version that takes an index is chosen and the second element is removed. If we want to remove the first element (with value 1), we have to call the remove() method like this: list.remove(new Integer(1)).


4. The correct answer is D.
HashSet is an implementation of Set. Objects used as keys of a TreeMap are required to implement the hashCode() method. Values are not required to implement anything.