Chapter 20 Flashcards
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
B.
the Serializable interfaces
For an instance of Collection, you can obtain its iterator using \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_. A. c.getIterator() B. c.iterator() C. c.iterators() D. c.iterable()
B.
c.iterator()
The iterator() method is defined in the \_\_\_\_\_\_\_\_\_\_ interface. A. Iterator B. Collection C. Iterable D. ArrayList
C.
Iterable
The iterator() method returns an instance of the \_\_\_\_\_\_\_\_\_\_ interface. A. Iterator B. Collection C. Iterable D. ArrayList
A.
Iterator
You can use a for-each loop to traverse all elements in a container object that implements \_\_\_\_\_. A. Iterator B. Collection C. Iterable D. ArrayList
C.
Iterable
Suppose list1 is an ArrayList and list2 is a LinkedList. Both contains 1 million double values. Analyze the following code:
A: for(inti=0;i
A.
Code fragment A runs faster than code fragment B.
Suppose list is a LinkedList that contains 1 million int values. Analyze the following code:
A: for(inti=0;i
B.
Code fragment B runs faster than code fragment A.
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)
D.
x.contains(element)
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()
D.
x.size()
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
B.
x.remove(element)
What is the printout of the following code?
List list =newArrayList<>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); for(inti =0; i < list.size(); i++) System.out.print(list.remove(i)); A. ABCD B. AB C. AC D. AD E. ABC
C.
AC
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]
A.
[1, 2, 2, 3, 5, 6]
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]
E.
[2, 3, 6]
Suppose a list contains {“red”, “green”, “red”, “green”}. What is the list after the following code?
list.remove("red"); A. {"red", "green", "red", "green"} B. {"green", "red", "green"} C. {"green", "green"} D. {"red", "green", "green"}
B.
{“green”, “red”, “green”}
Suppose a list contains {“red”, “green”, “red”, “green”}. What is the list after the following code?
String element ="red"; for(inti =0; i < list.size(); i++) if(list.get(i).equals(element)) { list.remove(element); i--; } A. {"red", "red", "green"} B. {"red", "green"} C. {"green", "green"} D. {"green"} E. {}
C.
{“green”, “green”}