Chapter 23
The Collection interface is the base interface for _____________. A. Set B. List C. ArrayList D. LinkedList E. Map
A,B,C,D
Which of the data types below does not allow duplicates? A. Set B. List C. Vector D. Stack E. LinkedList
A: Set
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)
C: s1.retainAll(s2)
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]
D: [2,3,6]
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]
D: [2]
The output of the following code is ____________. LinkedHashSet<String> set1 = new LinkedHashSet<String>(); set1.add("New York"); LinkedHashSet<String> set2 = (LinkedHashSet<String>)(set1.clone()); set1.add("Atlanta"); set2.add("Dallas"); System.out.println(set2); A. [New York] B. [New York, Atlanta] C. [New York, Atlanta, Dallas] D. [New York, Dallas]
D: [New York, Dallas]
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)
D: s1.removeAll(s2)
Analyze the following code. import java.util.*; public class Test { public static void main(String[] args) throws Exception { Set<String> set = new TreeSet<String>(); 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.
E: Explanation: first() is defined in TreeSet. To compile this program, replace Set set = new TreeSet() with TreeSet set = new TreeSet().
Which of the following data types does not implement the Collection interface? A. HashSet B. TreeSet C. ArrayList D. LinkedList E. Map
E: Map
The Map is the base interface for _____________. A. TreeMap B. HashMap C. LinkedHashMap D. ArrayList E. LinkedList
A,B,C
import java.util.*; public class Test { public static void main(String[] args) throws Exception { TreeSet<String> set = new TreeSet<String>(); 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.
A: The program displays red
import java.util.*; public class Test { public static void main(String[] args) throws Exception { TreeSet<String> set = new TreeSet<String>(); 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
A: The program displays red
If two objects o1 and o2 are equal, what are the values for o1.equals(o2) and o1.hashCode() == o2.hashCode()? A. true true B. true false C. false true D. false false
A: True true
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
ACD Explanation: TreeSet has the first(), last(), headSet(toElement), and tailSet(fromElement) methods.
To empty a Collection or a Map, you use the __________ method. A. empty B. clear C. zero D. setEmpty
B: Clear
Suppose your program frequently tests whether a student is in a soccer team and also need to know the student?s information such as phone number, address, and age, what is the best data structure to store the students in a soccer team? A. ArrayList B. HashMap C. TreeMap D. LinkedList E. HashSet
B: HashMap
Suppose your program frequently tests whether a student is in a soccer team, what is the best data structure to store the students in a soccer team? A. ArrayList B. HashSet C. LinkedList E. Vector
B: HashSet
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
B: LinkedHashSet
import java.util.*; public class Test { public static void main(String[] args) throws Exception { TreeSet<String> set = new TreeSet<String>(); 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
B: The program displays Blue
Which of the data types 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
B: TreeSet
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]
B: [1,2,3,5,6]
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)
C: s1.addAll(s2)
To get an iterator from a set, you may use the __________ method. A. getIterator B. findIterator C. iterator D. iterators
C: Iterator
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]
C: [1,5]
The output of the following code is ____________. LinkedHashSet<String> set1 = new LinkedHashSet<String>(); set1.add("New York"); LinkedHashSet<String> set2 = set1; set1.add("Atlanta"); set2.add("Dallas"); System.out.println(set2); A. [New York] B. [New York, Atlanta] C. [New York, Atlanta, Dallas] D. [New York, Dallas]
C: [New York, Atlanta, Dallas]