Chapter 20 Flashcards

1
Q
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
A

B.

the Serializable interfaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
For an instance of Collection, you can obtain its iterator using \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_.
		A.
c.getIterator()
		B.
c.iterator()
		C.
c.iterators()
		D.
c.iterable()
A

B.

c.iterator()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
The iterator() method is defined in the \_\_\_\_\_\_\_\_\_\_ interface.
		A.
Iterator
		B.
Collection
		C.
Iterable
		D.
ArrayList
A

C.

Iterable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
The iterator() method returns an instance of the \_\_\_\_\_\_\_\_\_\_ interface.
		A.
Iterator
		B.
Collection
		C.
Iterable
		D.
ArrayList
A

A.

Iterator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
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
A

C.

Iterable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A.

Code fragment A runs faster than code fragment B.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Suppose list is a LinkedList that contains 1 million int values. Analyze the following code:

A:
for(inti=0;i
A

B.

Code fragment B runs faster than code fragment A.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
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)
A

D.

x.contains(element)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
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()
A

D.

x.size()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
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
A

B.

x.remove(element)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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
A

C.

AC

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
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

A.

[1, 2, 2, 3, 5, 6]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
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]
A

E.

[2, 3, 6]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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"}
A

B.

{“green”, “red”, “green”}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.
{}
A

C.

{“green”, “green”}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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.
{}
A

C.

{“green”, “green”}

17
Q

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]
A

B.

[1, 2]

18
Q

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

A.

Collection

19
Q

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
A

B.

true false

20
Q

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]]
A

C.

[[Sarah, 34], [Jill, 50], [Peter, 65]] [[Jill, 50], [Peter, 65], [Sarah, 34]]

21
Q
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"})
A

B.

Collections.sort(lst)

22
Q

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.

A

D.

Collections.nCopies(int, Object) returns a new list that consists of n copies of the object.

23
Q

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”})

A

C.

Arrays.asList(new String[]{“red”, “green”, “blue”})

24
Q
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
A

D.

Collections.max(Arrays.asList(names))

25
Q
Which data type should you use if you want to store duplicate elements and be able to insert or delete elements at the beginning of the list?
		A.
ArrayList
		B.
LinkedList
		C.
Vector
		D.
Set
		E.
Stack
A

B.

LinkedList

26
Q
The methods for modifying element in the \_\_\_\_\_\_\_\_\_\_\_ class are synchronized.
		A.
ArrayList
		B.
LinkedList
		C.
TreeMap
		D.
Vector
		E.
HashSet
A

D.

Vector

27
Q
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()
A

A.

poll()

28
Q
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()
A

B.

remove()

29
Q
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()
A

C.

peek()

30
Q
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()
A

D.

element()

31
Q

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.

A

E.

java.util.PriorityQueue implements the java.util.Queue interface.

32
Q

Analyze the following code:

importjava.util.*;
	publicclassTest{
	publicstaticvoidmain(String[] args) {
	PriorityQueue queue =
	newPriorityQueue(
	Arrays.asList(60,10,50,30,40,20));
	
	for(inti: 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
A

D.

There is no guarantee that the program displays 10 20 30 40 50 60

33
Q

Analyze the following code:

importjava.util.*;
	publicclassTest{
	publicstaticvoidmain(String[] args) {
	PriorityQueue queue =
	newPriorityQueue(
	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
A

B.

The program displays 10 20 30 40 50 60

34
Q

What is list after the following code is executed?

	ArrayListlist=newArrayList<>();
	list.add(1);
	list.add(2);
	list.add(3);
	list.add(4);
	list.add(5);
	list.remove(2);
	System.out.println(list);
		A.
[1, 2, 3, 4, 5]
		B.
[2, 3, 4, 5]
		C.
[1, 3, 4, 5]
		D.
[1, 2, 4, 5]
		E.
[1, 2, 3, 4]
A

D.

[1, 2, 4, 5]