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”}
Suppose a list contains {“red”, “green”, “red”, “green”}. What is the list after the following code?
String element ="red"; for(inti = list.size() -1; i >=0; i--) if(list.get(i).equals(element)) list.remove(element); A. {"red", "red", "green"} B. {"red", "green"} C. {"green", "green"} D. {"green"} E. {}
C.
{“green”, “green”}
What is the output of the following code?
ArrayList list =newArrayList<>(); list.add(1); list.add(2); list.add(3); list.remove(2); System.out.println(list); A. [1, 2, 3] B. [1, 2] C. [1] D. [1, 3] E. [2, 3]
B.
[1, 2]
To remove all the elements in the list in the following code, replace the underlined blank space with __________.
importjava.util.*; publicclassTest{ publicstaticvoidmain(String[] args) { \_\_\_\_\_\_\_\_\_\_\_\_\_\_ list =newArrayList<>(); list.add(0); list.add(1); list.add(2); for(inti =0; i
A.
Collection
What is the output of the following code?
importjava.util.*;
publicclassTest{ publicstaticvoidmain(String[] args) { List list1 =newArrayList<>(); list1.add("Atlanta"); list1.add("Macon"); list1.add("Savanna"); List list2 =newArrayList<>(); list2.add("Atlanta"); list2.add("Macon"); list2.add("Savanna"); List list3 =newArrayList<>(); list3.add("Macon"); list3.add("Savanna"); list3.add("Atlanta"); System.out.println(list1.equals(list2) +" "+ list1.equals(list3)); } } A. true true B. true false C. false false D. false true
B.
true false
What is the output of the following code?
importjava.util.*; publicclassTest{ publicstaticvoidmain(String[] args) { ArrayList list =newArrayList<>(); list.add(newStudent("Peter",65)); list.add(newStudent("Jill",50)); list.add(newStudent("Sarah",34)); Collections.sort(list);........ ... A. [[Sarah, 34], [Jill, 50], [Peter, 65]] [[Sarah, 34], [Jill, 50], [Peter, 65]] B. [[Jill, 50], [Peter, 65], [Sarah, 34]] [[Jill, 50], [Peter, 65], [Sarah, 34]] C. [[Sarah, 34], [Jill, 50], [Peter, 65]] [[Jill, 50], [Peter, 65], [Sarah, 34]] D. [[Jill, 50], [Peter, 65], [Sarah, 34]] [[Sarah, 34], [Jill, 50], [Peter, 65]]
C.
[[Sarah, 34], [Jill, 50], [Peter, 65]] [[Jill, 50], [Peter, 65], [Sarah, 34]]
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"})
B.
Collections.sort(lst)
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.
D.
Collections.nCopies(int, Object) returns a new list that consists of n copies of the object.
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”})
C.
Arrays.asList(new String[]{“red”, “green”, “blue”})
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
D.
Collections.max(Arrays.asList(names))