Introduction to Java Programming, Sixth Edition, Y. Daniel Liang
Chapter 22 Java Collections Framework
Section 22.1 Introduction
1
The Collection interface is the base interface for _____________.
A.
Set
B.
List
C.
ArrayList
D.
LinkedList
E.
Map
2
The Map is the base interface for _____________.
A.
TreeMap
B.
HashMap
C.
LinkedHashMap
D.
ArrayList
E.
LinkedList
3
All the concrete classes in the Java Collections Framework implement _____________.
A.
the Cloneable interface
B.
the Serializable interfaces
C.
the Comparable interface
D.
the Comparator interface
Section 22.2 The Collection Interface and the AbstractCollection Class
4
Which of the following statements are true?
A.
The Collection interface is the root interface for manipulating a collection of objects.
B.
The Collection interface provides the basic operations for adding and removing elements in a collection.
C.
The AbstractCollection class is a convenience class that provides partial implementation for the Collection interface.
D.
Some of the methods in the Collection interface cannot be implemented in the concrete subclass. In this case, the method would throw java.lang.UnsupportedOperationException, a subclass of RuntimeException.
E.
All interfaces and classes in the Collections framework are declared using generic type in JDK 1.5.
5
Which of the following methods are in the Collection interface?
A.
clear()
B.
isEmpty()
C.
size()
D.
getSize()
6
Which of the following methods are in the Collection interface?
A.
add(o: E)
B.
addAll(c: Collection<? extends E>)
C.
contains(o: Object): boolean
D.
containsAll(c: Collection<?>): boolean
7
Which of the following methods are in the Collection interface?
A.
remove(o: E): boolean
B.
removeAll(c: Collection<?>): boolean
C.
delete(o: E): boolean
D.
deleteAll(c: Collection<?>): boolean
8
3 Sets
8. Which of the data type below does not allow duplicates?
A.
Set
B.
List
C.
Vector
D.
Stack
E.
LinkedList
9
Which of the following data types does not implement the Collection interface?
A.
HashSet
B.
TreeSet
C.
ArrayList
D.
LinkedList
E.
Map
10
Which of the data type below could be used to store elements in their natural order based on the compareTo method.
A.
HashSet
B.
TreeSet
C.
LinkedHashSet
D.
Collection
E.
Set
11
Which of the following data types have iterators?
A.
HashSet
B.
TreeSet
C.
ArrayList
D.
LinkedList
E.
LinkedHashSet
12
To get an iterator from a set, you may use the __________ method.
A.
getIterator
B.
findIterator
C.
iterator
D.
iterators
13
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.addAll(s2), s1 is __________.
A.
[1, 2, 2, 3, 5, 6]
B.
[1, 2, 3, 5, 6]
C.
[1, 5]
D.
[2]
14
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.addAll(s2), s2 is __________.
A.
[1, 2, 2, 3, 5, 6]
B.
[1, 2, 3, 5, 6]
C.
[1, 5]
D.
[2, 3, 6]
E.
[2]
15
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.removeAll(s2), s1 is __________.
A.
[1, 2, 2, 3, 5, 6]
B.
[1, 2, 3, 5, 6]
C.
[1, 5]
D.
[2]
16
Suppose set s1 is [1, 2, 5] and set s2 is [2, 3, 6]. After s1.retainAll(s2), s1 is __________.
A.
[1, 2, 2, 3, 5, 6]
B.
[1, 2, 3, 5, 6]
C.
[1, 5]
D.
[2]
17
Analyze the following code:
public class Test {
   public static void main(String[] args) {
     HastSet set1 = new HashSet();
     set1.add("red");
     Set set2 = set1.clone();
   }
}
A.
Line 5 is wrong because a HashSet object cannot be cloned.
B.
Line 5 has a syntax error because set1.clone() returns an Object. You have to cast it to Set in order to compile it.
C.
The program will be fine if set1.clone() is replaced by (Set)set1.clone()
D.
The program will be fine if set1.clone() is replaced by (Set)(set1.clone())
E.
The program will be fine if set1.clone() is replaced by (HashSet)(set1.clone())
18
Analyze the following code:
public class Test {
   public static void main(String[] args) {
     Set set1 = new HashSet();
     set1.add("red");
     Set set2 = set1.clone();
   }
}
A.
Line 5 is wrong because the declared type for set1 is Set and the clone method is protected in an instance of Set.
B.
The program will be fine if set1.clone() is replaced by (HashSet)set1.clone()
C.
The program will be fine if set1.clone() is replaced by (Set)((HashSet)set1).clone()
D.
The program will be fine if set1.clone() is replaced by (HashSet)((HashSet)set1).clone()
E.
The program will be fine if set1.clone() is replaced by (LinkedHashSet)((HashSet)set1).clone()
19
If you want to store non-duplicated objects in the order in which they are inserted, you should use ____________.
A.
HashSet
B.
LinkedHashSet
C.
TreeSet
D.
ArrayList
E.
LinkedList
20
Which of the following statements are true?
A.
All the methods in HashSet are inherited from the Collection interface.
B.
All the methods in TreeSet are inherited from the Collection interface.
C.
All the methods in LinkedHashSet are inherited from the Collection interface.
D.
All the methods in Set are inherited from the Collection interface.
E.
All the concrete classes of Collection have at least two constructors. One is the no-arg constructor that constructs an empty collection. The other constructs instances from a collection.
21
Which of the following is correct to perform the set union of two sets s1 and s2.
A.
s1.union(s2)
B.
s1 + s2
C.
s1.addAll(s2)
D.
s1.add(s2)
22
Which of the following is correct to perform the set difference of two sets s1 and s2.
A.
s1.difference(s2)
B.
s1 - s2
C.
s1.subtract(s2)
D.
s1.removeAll(s2)
23
Which of the following is correct to perform the set intersection of two sets s1 and s2.
A.
s1.intersect(s2)
B.
s1.join(s2)
C.
s1.retainAll(s2)
D.
s1.intersection(s2)
24
Analyze the following code.
import java.util.*;
public class Test {
   public static void main(String[] args) throws Exception {
     Set set = new TreeSet();
    
     set.add("Red");
     set.add("Green");
     set.add("Blue");
    
     System.out.println(set.first());
   }
}
A.
The program displays Red
B.
The program displays Blue
C.
The program displays Green
D.
The program may display Red, Blue, or Green.
E.
The program cannot compile, because the first() method is not defined in Set.
25
Analyze the following code.
import java.util.*;
public class Test {
   public static void main(String[] args) throws Exception {
     TreeSet set = new TreeSet();
    
     set.add("Red");
     set.add("Green");
     set.add("Blue");
    
     System.out.println(set.last());
   }
}
A.
The program displays Red
B.
The program displays Blue
C.
The program displays Green
D.
The program may display Red, Blue, or Green.
E.
The program cannot compile, because the first() method is not defined in Set.
26
Analyze the following code.
import java.util.*;
public class Test {
   public static void main(String[] args) throws Exception {
     TreeSet set = new TreeSet();
    
     set.add("Red");
     set.add("Yellow");
     set.add("Green");
     set.add("Blue");
     SortedSet temp = set.headSet("Purple");
    
     System.out.println(temp.first());
   }
}
A.
The program displays Red
B.
The program displays Blue
C.
The program displays Green
D.
The program displays Yellow
E.
The program displays Purple
27
Analyze the following code.
import java.util.*;
public class Test {
   public static void main(String[] args) throws Exception {
     TreeSet set = new TreeSet();
    
     set.add("Red");
     set.add("Yellow");
     set.add("Green");
     set.add("Blue");
     SortedSet temp = set.tailSet("Purple");
    
     System.out.println(temp.first());
   }
}
A.
The program displays Red
B.
The program displays Blue
C.
The program displays Green
D.
The program displays Yellow
E.
The program displays Purple
Section 22.4 The Comparator Interface
28
Which of the following statements are true?
A.
The Comparable interface contains the compareTo method with the signature "public int compareTo(Object)".
B.
The Comparator interface contains the compare method with the signature "public int compare(Object, Object)".
C.
A Comparable object can compare this object with the other object.
D.
A Comparator object contains the compare method that compares two objects.
Section 22.5 Lists
29
Which of the following statements are true?
A.
java.util.List inherits all the methods from java.util.Collection. Additionally, it contains new methods for manipulating a list.
B.
The AbstractList class provides a partial implementation for the List interface.
C.
ArrayList is a concrete implementation of List using an array.
D.
LinkedList is a concrete implementation of List using a linked list. LinkedList contains all the methods in List and additional new methods for manipulating a linked list.
E.
ListIterator is a subinterface of Iterator and it provides the methods to support bi-directional traversal of a list.
30
Which of the following statements are true?
A.
An ArrayList can grow automatically.
B.
An ArrayList can shrink automatically.
C.
You can reduce the capacity of an ArrayList by invoking the trimToSize() method on the list.
D.
You can reduce the capacity of a LinkedList by invoking the trimToSize() method on the list.
31
Which of the following methods are in java.util.List?
A.
add(int index, Object element)
B.
get(int index)
C.
set(int index, Object element)
D.
remove(int index)
E.
subList(int fromIndex, int toIndex)
32
Which of the following are true?
A.
You can insert an element anywhere is an arraylist.
B.
You can insert an element anywhere is a linked list.
C.
You can use a linked list to improve efficiency for adding and remove an element anywhere in a list.
D.
You should use an array list if your application does not require adding and remove an element anywhere in a list.
33
Which method do you use to test if an element is in a set or list named x?
A.
(element instanceof List) || (element instanceof Set)
B.
x.in(element)
C.
x.contain(element)
D.
x.contains(element)
E.
x.include(element)
34
Which method do you use to find the number of elements in a set or list named x?
A.
x.length()
B.
x.count()
C.
x.counts()
D.
x.size()
E.
x.sizes()
35
Which method do you use to remove an element from a set or list named x?
A.
x.delete(element)
B.
x.remove(element)
C.
x.deletes(element)
D.
x.removes(element)
E.
None of the above
36
What is the printout of the following code?
         List<String> list = new ArrayList<String>();
         list.add("A");
         list.add("B");
         list.add("C");
         list.add("D");
         for (int i = 0; i < list.size(); i++)
             System.out.print(list.remove(i));
A.
ABCD
B.
AB
C.
AC
D.
AD
E.
ABC
37
Suppose list list1 is [1, 2, 5] and list list2 is [2, 3, 6]. After list1.addAll(list2), list1 is __________.
A.
[1, 2, 2, 3, 5, 6]
B.
[1, 2, 3, 5, 6]
C.
[1, 5]
D.
[2]
38
Suppose list list1 is [1, 2, 5] and list list2 is [2, 3, 6]. After list1.addAll(list2), list2 is __________.
A.
[1, 2, 2, 3, 5, 6]
B.
[1, 2, 3, 5, 6]
C.
[1, 5]
D.
[2]
E.
[2, 3, 6]
Section 22.6 Static Methods for Lists and Collections
39
Which of the following is correct to sort the elements in a list lst?
A.
lst.sort()
B.
Collections.sort(lst)
C.
Arrays.sort(lst)
D.
new LinkedList(new String[]{"red", "green", "blue"})
40
You can use the methods in the Collections class to
A.
find the maximum object in a collection based on the compareTo method.
B.
find the maximum object in a collection using a Comparator object.
C.
sort a collection.
D.
shuffle a collection.
E.
do a binary search on a collection.
41
Which of the following statements are true?
A.
Collections.shuffle(list) returns a new list while the original list is not changed.
B.
Collections.reverse(list) returns a new list while the original list is not changed.
C.
Collections.sort(list) returns a new list while the original list is not changed.
D.
Collections.nCopies(int, Object) returns a new list that consists of n copies of the object.
42
Which of the following statements are true?
A.
Collections.shuffle(list) randomly reorders the elements in the list.
B.
Collections.shuffle(list, Random) randomly reorders the elements in the list with a specified Random object.
C.
If list1 and list2 are identical, the two lists may be different after invoking Collections.sort(list1) and Collections.sort(list2).
D.
If list1 and list2 are indentical, the two lists are still identical after invoking Collections.sort(list1, new Random(3)) and Collections.sort(list2, new Random(3)) with the same Random object.
43
Which of the following is correct to create a list from an array?
A.
new List({"red", "green", "blue"})
B.
new List(new String[]{"red", "green", "blue"})
C.
Arrays.asList(new String[]{"red", "green", "blue"})
D.
new ArrayList(new String[]{"red", "green", "blue"})
E.
new LinkedList(new String[]{"red", "green", "blue"})
44
Which of the following is correct to create a list from an array?
A.
new List({"red", "green", "blue"})
B.
new List(new String[]{"red", "green", "blue"})
C.
Arrays.asList(new String[]{"red", "green", "blue"})
D.
new ArrayList(new String[]{"red", "green", "blue"})
E.
new LinkedList(new String[]{"red", "green", "blue"})
45
To create a set that consists of string elements "red", "green", and "blue", use
A.
new HashSet({"red", "green", "blue"})
B.
new HashSet(new String[]{"red", "green", "blue"})
C.
new HashSet(Arrays.asList(new String[]{"red", "green", "blue"}))
D.
new LinkedHashSet(Arrays.asList(new String[]{"red", "green", "blue"}))
E.
new Set(Arrays.asList(new String[]{"red", "green", "blue"}))
46
To find a maximum object in an array of strings (e.g., String[] names = {"red", "green", "blue"}), use
A.
Arrays.max(names)
B.
Arrays.sort(names)
C.
Collections.max(names)
D.
Collections.max(Arrays.asList(names))
E.
None of the above
47
You can use the methods in the Arrays class to
A.
find the maximum object in an array based on the compareTo method.
B.
find the maximum object in an array using a Comparator object.
C.
sort an array.
D.
shuffle an array.
E.
do a binary search on an array.
Section 22.7 The Vector and Stack Classes
48
Which data type should you use if you want to store duplicate elements and be able to insert or delete elements anywhere efficiently.
A.
ArrayList
B.
LinkedList
C.
Vector
D.
Set
E.
Stack
49
java.util.Vector is a subtype of __________.
A.
java.util.ArrayList
B.
java.util.LinkedList
C.
java.util.AbstractList
D.
java.util.Vector
E.
java.util.List
50
The methods for modifying element in the ___________ class are synchronized.
A.
ArrayList
B.
LinkedList
C.
TreeMap
D.
Vector
E.
HashSet
51
java.util.Stack is a subclass of __________.
A.
java.util.ArrayList
B.
java.util.LinkedList
C.
java.util.AbstractList
D.
java.util.Vector
E.
java.util.List
Section 22.8 Queues and Priority Queues
52
The __________ method in the Queue interface retrieves and removes the head of this queue, or null if this queue is empty.
A.
poll()
B.
remove()
C.
peek()
D.
element()
53
The __________ method in the Queue interface retrieves and removes the head of this queue and throws an exception if this queue is empty.
A.
poll()
B.
remove()
C.
peek()
D.
element()
54
The __________ method in the Queue interface retrieves, but does not remove, the head of this queue, returning null if this queue is empty.
A.
poll()
B.
remove()
C.
peek()
D.
element()
55
The __________ method in the Queue interface retrieves, but does not remove, the head of this queue, throwing an exception if this queue is empty.
A.
poll()
B.
remove()
C.
peek()
D.
element()
56
Which of the following statements are true?
A.
java.util.LinkedList implements the java.util.Queue interface.
B.
java.util.ArrayList implements the java.util.Queue interface.
C.
java.util.HashSet implements the java.util.Queue interface.
D.
java.util.LinkedHashSet implements the java.util.Queue interface.
E.
java.util.PriorityQueue implements the java.util.Queue interface.
57
Which of the following statements are true?
A.
A PriorityQueue orders its elements according to their natural ordering using the Comparable interface if no Comparator is specified.
B.
A PriorityQueue orders its elements according to the Comparator if a Comparator is specified in the constructor.
C.
The priority of a PriorityQueue cannot be changed once a PriorityQueue is created.
D.
The priority of a PriorityQueue cannot be reversed once a PriorityQueue is created.
58
Analyze the following code:
import java.util.*;
public class Test {
   public static void main(String[] args) {
     PriorityQueue<Integer> queue =
       new PriorityQueue<Integer>(
         Arrays.asList(60, 10, 50, 30, 40, 20));
    
     for (int i: queue)
       System.out.print(i + " ");
   }
}
A.
The program displays 60 10 50 30 40 20
B.
The program displays 10 20 30 40 50 60
C.
The program displays 60 50 40 30 20 10
D.
There is no guarantee that the program displays 10 20 30 40 50 60
59
Analyze the following code:
import java.util.*;
public class Test {
   public static void main(String[] args) {
     PriorityQueue<Integer> queue =
       new PriorityQueue<Integer>(
         Arrays.asList(60, 10, 50, 30, 40, 20));
    
     while (!queue.isEmpty())
       System.out.print(queue.poll() + " ");
   }
}
A.
The program displays 60 10 50 30 40 20
B.
The program displays 10 20 30 40 50 60
C.
The program displays 60 50 40 30 20 10
D.
There is no guarantee that the program displays 10 20 30 40 50 60
Section 22.9 (Optional) Maps
60
To empty a Collection or a Map, you use the __________ method.
A.
empty
B.
clear
C.
zero
D.
setEmpty
61
Which of the following are correct methods in Map?
A.
put(Object key, Object value)
B.
put(Object value, Object key)
C.
get(Object key)
D.
get(int index)
62
Which of the following are correct methods in Map?
A.
containsKey(Object key)
B.
containsValue(Object value)
C.
remove(Object key)
D.
remove(int index)
E.
isEmpty()
63
The elements in ________ are sorted.
A.
TreeSet
B.
List
C.
TreeMap
D.
HashSet
E.
LinkedHashSet
64
Analyze the following code:
public class Test {
   public static void main(String[] args) {
     Map map = new HashMap();
     map.put("123", "John Smith");
     map.put("111", "George Smith");
     map.put("123", "Steve Yao");
     map.put("222", "Steve Yao");
   }
}
A.
After all the four entries are added to the map, "123" is a key that corresponds to the value "John Smith".
B.
After all the four entries are added to the map, "123" is a key that corresponds to the value "Steve Yao".
C.
After all the four entries are added to the map, "Steve Yao" is a key that corresponds to the value "222".
D.
After all the four entries are added to the map, "John Smith" is a key that corresponds to the value "123".
E.
A runtime error occurs because two entries with the same key "123" are added to the map.