Deck 15: The Java Collections Framework
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/110
Play
Full screen (f)
Deck 15: The Java Collections Framework
1
A collection that remembers the order of items, and allows items to be added and removed only at one end is called a ____.
A) list
B) stack
C) set
D) queue
A) list
B) stack
C) set
D) queue
B
2
A stack is a collection that ____.
A) remembers the order of elements, and allows elements to be added and removed only at one end.
B) does not remember the order of elements but allows elements to be added in any position.
C) remembers the order of elements and allows elements to be inserted in any position.
D) remembers the order of elements and allows elements to be inserted only at one end and removed only at the other end.
A) remembers the order of elements, and allows elements to be added and removed only at one end.
B) does not remember the order of elements but allows elements to be added in any position.
C) remembers the order of elements and allows elements to be inserted in any position.
D) remembers the order of elements and allows elements to be inserted only at one end and removed only at the other end.
A
3
Rather than storing values in an array, a linked list uses a sequence of ____.
A) indexes
B) nodes
C) elements
D) accessors
A) indexes
B) nodes
C) elements
D) accessors
B
4
Which nodes need to be updated when we insert a new node to become the fourth node from the beginning of a doubly-linked list?
A) The current third node.
B) The current third and fourth nodes.
C) The current first node.
D) The current fourth and fifth nodes.
A) The current third node.
B) The current third and fourth nodes.
C) The current first node.
D) The current fourth and fifth nodes.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
5
We might choose to use a linked list over an array list when we will not require frequent ____.
I random access
II inserting new elements
III removing of elements
A) I
B) II
C) III
D) II and III
I random access
II inserting new elements
III removing of elements
A) I
B) II
C) III
D) II and III
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
6
A linear search only requires ____ access.
A) sequential
B) random
C) sorted
D) arbitrary
A) sequential
B) random
C) sorted
D) arbitrary
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
7
Which of the following statements about linked lists is correct?
A) Once you have located the correct position, adding elements in the middle of a linked list is inefficient.
B) Visiting the elements of a linked list in random order is efficient.
C) When a node is removed, all nodes after the removed node must be moved down.
D) Linked lists should be used when you know the correct position and need to insert and remove elements efficiently.
A) Once you have located the correct position, adding elements in the middle of a linked list is inefficient.
B) Visiting the elements of a linked list in random order is efficient.
C) When a node is removed, all nodes after the removed node must be moved down.
D) Linked lists should be used when you know the correct position and need to insert and remove elements efficiently.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
8
A collection without an intrinsic order is called a ____.
A) list
B) stack
C) set
D) queue
A) list
B) stack
C) set
D) queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
9
Consider the following code snippet: LinkedList words = new LinkedList();
Words.addLast("abc");
Words.addLast("def");
Words.addLast("ghi");
System.out.print(words.removeLast());
System.out.print(words.removeFirst());
System.out.print(words.removeLast());
What will this code print when it is executed?
A) abcdefghi
B) ghiabcdef
C) abcghidef
D) defghiabc
Words.addLast("abc");
Words.addLast("def");
Words.addLast("ghi");
System.out.print(words.removeLast());
System.out.print(words.removeFirst());
System.out.print(words.removeLast());
What will this code print when it is executed?
A) abcdefghi
B) ghiabcdef
C) abcghidef
D) defghiabc
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
10
Which data structure would best be used for keeping track of a growing set of groceries to be purchased at the food market?
A) queue
B) stack
C) list
D) array
A) queue
B) stack
C) list
D) array
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
11
What is included in a linked list node?
I a reference to its neighboring nodes
II an array reference
III a data element
A) I
B) II
C) II and III
D) I and III
I a reference to its neighboring nodes
II an array reference
III a data element
A) I
B) II
C) II and III
D) I and III
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
12
What type of access does a LinkedList provide for its elements?
A) sequential
B) semi-random
C) random
D) sorted
A) sequential
B) semi-random
C) random
D) sorted
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
13
A collection that allows speedy insertion and removal of already-located elements in the middle of it is called a ____.
A) linked list
B) stack
C) set
D) queue
A) linked list
B) stack
C) set
D) queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
14
Which of the following algorithms would be efficiently executed using a LinkedList?
A) Tracking paths in a maze.
B) Binary search.
C) Remove first n/ 2 elements from a list of n elements.
D) Read n / 2 elements in random order from a list of n elements.
A) Tracking paths in a maze.
B) Binary search.
C) Remove first n/ 2 elements from a list of n elements.
D) Read n / 2 elements in random order from a list of n elements.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
15
The ArrayList class implements the ____.
A) Queue interface.
B) Set interface.
C) List interface.
D) Stack interface.
A) Queue interface.
B) Set interface.
C) List interface.
D) Stack interface.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
16
A list is a collection that ____.
A) should be used when you need to remember the order of elements in the collection.
B) allows items to be added at one end and removed at the other end.
C) does not allow elements to be inserted in any position.
D) manages associations between keys and values.
A) should be used when you need to remember the order of elements in the collection.
B) allows items to be added at one end and removed at the other end.
C) does not allow elements to be inserted in any position.
D) manages associations between keys and values.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
17
A collection that allows items to be added only at one end and removed only at the other end is called a ____.
A) list
B) stack
C) set
D) queue
A) list
B) stack
C) set
D) queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
18
Select an appropriate expression to complete the following code segment, which is designed to print a message if the string stored in name is part of the players collection. Collection players = new ArrayList();
// code to add elements to the collection here
If ______________________________________
System.out.print(name + " is one of the players in the collection.");
A) (players.indexOf(name))
B) (players.contains(name))
C) (players.search(name))
D) (players.equals(name))
// code to add elements to the collection here
If ______________________________________
System.out.print(name + " is one of the players in the collection.");
A) (players.indexOf(name))
B) (players.contains(name))
C) (players.search(name))
D) (players.equals(name))
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
19
A queue is a collection that ____.
A) remembers the order of elements, and allows elements to be added and removed only at one end.
B) does not remember the order of elements but allows elements to be added in any position.
C) remembers the order of elements and allows elements to be inserted in any position.
D) remembers the order of elements and allows elements to be inserted only at one end and removed only at the other end.
A) remembers the order of elements, and allows elements to be added and removed only at one end.
B) does not remember the order of elements but allows elements to be added in any position.
C) remembers the order of elements and allows elements to be inserted in any position.
D) remembers the order of elements and allows elements to be inserted only at one end and removed only at the other end.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
20
A binary search requires ____ access.
A) sequential
B) random
C) sorted
D) arbitrary
A) sequential
B) random
C) sorted
D) arbitrary
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
21
Consider the following code snippet: LinkedList words = new LinkedList();
Words.addFirst("abc");
Words.addLast("def");
Words.addFirst("ghi");
System.out.print(words.removeLast());
System.out.print(words.removeFirst());
System.out.print(words.removeLast());
What will this code print when it is executed?
A) abcdefghi
B) ghiabcdef
C) abcghidef
D) defghiabc
Words.addFirst("abc");
Words.addLast("def");
Words.addFirst("ghi");
System.out.print(words.removeLast());
System.out.print(words.removeFirst());
System.out.print(words.removeLast());
What will this code print when it is executed?
A) abcdefghi
B) ghiabcdef
C) abcghidef
D) defghiabc
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
22
When using a list iterator, on which condition will the IllegalStateException be thrown?
A) Calling remove after calling next.
B) Calling add after calling previous.
C) Calling remove after calling add.
D) Calling previous after calling previous.
A) Calling remove after calling next.
B) Calling add after calling previous.
C) Calling remove after calling add.
D) Calling previous after calling previous.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
23
You use a(n) ____ to access elements inside a linked list.
A) accessor
B) index
C) list iterator
D) queue
A) accessor
B) index
C) list iterator
D) queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
24
What can a generic class be parameterized for?
A) properties
B) iterators
C) type
D) methods
A) properties
B) iterators
C) type
D) methods
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
25
Assume you have created a linked list name myList that currently holds some number of String objects. Which of the following statements correctly removes an element from the end of myList?
A) myList.remove();
B) myList.removeLast();
C) myList.getLast();
D) myList.pop();
A) myList.remove();
B) myList.removeLast();
C) myList.getLast();
D) myList.pop();
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
26
The term ____ is used in computer science to describe an access pattern in which the elements are accessed in arbitrary order.
A) sequential access
B) random access
C) sorted access
D) arbitrary access
A) sequential access
B) random access
C) sorted access
D) arbitrary access
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
27
Select an appropriate expression to complete the following method, which is designed to visit the elements in theList and replace each occurrence of the string "hello" with the string "goodbye". public static void helloGoodbye(LinkedList theList)
{
ListIterator iterator = theList.listIterator();
While (iterator.hasNext())
{
If (iterator.next().equals("hello"))
{
_____________________________
}
}
}
A) iterator.replace("hello", "goodbye");
B) iterator.next() = "goodbye";
C) iterator.previous("goodbye");
D) iterator.set("goodbye");
{
ListIterator
While (iterator.hasNext())
{
If (iterator.next().equals("hello"))
{
_____________________________
}
}
}
A) iterator.replace("hello", "goodbye");
B) iterator.next() = "goodbye";
C) iterator.previous("goodbye");
D) iterator.set("goodbye");
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
28
Which of the following statements about the LinkedList class is correct?
A) When you use the add method, the new element is inserted before the iterator, and the iterator position is advanced by one position.
B) When you use the add method, the new element is inserted after the iterator, and the iterator position is advanced by one position.
C) When you use the add method, the new element is inserted before the iterator, and the iterator position is not moved
D) When you use the add method, the new element is inserted after the iterator, and the iterator position is not moved.
A) When you use the add method, the new element is inserted before the iterator, and the iterator position is advanced by one position.
B) When you use the add method, the new element is inserted after the iterator, and the iterator position is advanced by one position.
C) When you use the add method, the new element is inserted before the iterator, and the iterator position is not moved
D) When you use the add method, the new element is inserted after the iterator, and the iterator position is not moved.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
29
Select an appropriate expression to complete the following code segment, which is designed to print a message if the string stored in name is the first element of the players linked list. LinkedList players = new LinkedList();
// code to add elements to the linked list
If ______________________________________
System.out.print(name + " is the first player on the list.");
A) (players.indexOf(name) == 1)
B) (players.contains(name))
C) (players.getFirst().equals(name))
D) (players[0].equals(name))
// code to add elements to the linked list
If ______________________________________
System.out.print(name + " is the first player on the list.");
A) (players.indexOf(name) == 1)
B) (players.contains(name))
C) (players.getFirst().equals(name))
D) (players[0].equals(name))
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
30
What is the meaning of the type parameter E, in the LinkedList code fragment?
A) The elements of the linked list are of class E.
B) The elements of the linked list are of any subclass of class E.
C) The elements of the linked list are any type supplied to the constructor.
D) The elements of the linked list are of class Object.
A) The elements of the linked list are of class E.
B) The elements of the linked list are of any subclass of class E.
C) The elements of the linked list are any type supplied to the constructor.
D) The elements of the linked list are of class Object.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
31
Which method is NOT part of the ListIterator generic class?
A) hasNext
B) hasMore
C) hasPrevious
D) add
A) hasNext
B) hasMore
C) hasPrevious
D) add
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
32
A linked list ____ encapsulates a position anywhere inside the linked list.
A) accessor
B) index
C) operation
D) iterator
A) accessor
B) index
C) operation
D) iterator
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
33
Which Java package contains the LinkedList class?
A) java.lang
B) java.util
C) java.collections
D) java.io
A) java.lang
B) java.util
C) java.collections
D) java.io
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
34
When using a list iterator, on which condition will the NoSuchElementException be thrown?
A) Calling next when you are past the end of the list.
B) Calling next when the iterator points to the last element.
C) Calling remove after calling add.
D) Calling remove after calling previous.
A) Calling next when you are past the end of the list.
B) Calling next when the iterator points to the last element.
C) Calling remove after calling add.
D) Calling remove after calling previous.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
35
A(n) ____ is a data structure used for collecting a sequence of objects that allows efficient addition and removal of already-located elements in the middle of the sequence.
A) stack
B) queue
C) linked list
D) priority queue
A) stack
B) queue
C) linked list
D) priority queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
36
Consider the code snippet shown below. Assume that employeeNames is an instance of type LinkedList. for (String name : employeeNames)
{
// Do something with name here
}
Which element(s) of employeeNames does this loop process?
A) no elements
B) all elements
C) elements meeting a condition
D) the most recently added elements
{
// Do something with name here
}
Which element(s) of employeeNames does this loop process?
A) no elements
B) all elements
C) elements meeting a condition
D) the most recently added elements
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
37
Which method is NOT part of the ListIterator interface?
A) delete
B) add
C) next
D) previous
A) delete
B) add
C) next
D) previous
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
38
Assume you have created a linked list named myList that currently holds some number of String objects. Which of the following statements correctly adds a new element to the beginning of myList?
A) myList.addFirst("Harry");
B) myList.add("Harry");
C) myList.insert("Harry");
D) myList.put("Harry");
A) myList.addFirst("Harry");
B) myList.add("Harry");
C) myList.insert("Harry");
D) myList.put("Harry");
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
39
When using a list iterator, on which condition will the IllegalStateException be thrown?
A) Calling remove after calling next.
B) Calling next after calling previous.
C) Calling remove after calling remove.
D) Calling remove after calling previous.
A) Calling remove after calling next.
B) Calling next after calling previous.
C) Calling remove after calling remove.
D) Calling remove after calling previous.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
40
Select an appropriate expression to complete the method below. The method should return the number of times that the string stored in name appears in theList. public static int count(LinkedList theList, String name)
{
Int number = 0;
Iterator iter = theList.iterator();
While (______________________)
{
If (iter.next().equals(name))
{
Number++;
}
}
Return number;
}
A) iter.hasNext()
B) iter.next() != null
C) theList.hasNext()
D) theList.next() != null
{
Int number = 0;
Iterator
While (______________________)
{
If (iter.next().equals(name))
{
Number++;
}
}
Return number;
}
A) iter.hasNext()
B) iter.next() != null
C) theList.hasNext()
D) theList.next() != null
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
41
Which of the following statements about manipulating objects in a set is correct?
A) If you try to add an element that already exists, an exception will occur.
B) If you try to remove an element that does not exist, an exception will occur.
C) You can add an element at the position indicated by an iterator.
D) A set iterator visits elements in the order in which the set implementation keeps them.
A) If you try to add an element that already exists, an exception will occur.
B) If you try to remove an element that does not exist, an exception will occur.
C) You can add an element at the position indicated by an iterator.
D) A set iterator visits elements in the order in which the set implementation keeps them.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
42
A linked list allows ____ access, but you need to ask the list for an iterator.
A) sequential
B) random
C) sorted
D) arbitrary
A) sequential
B) random
C) sorted
D) arbitrary
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
43
Assume that you have declared a set named mySet to hold String elements. Which of the following statements will correctly remove an element from mySet?
A) mySet.get("apple");
B) mySet.remove("apple");
C) mySet.pop("apple");
D) mySet.delete("apple");
A) mySet.get("apple");
B) mySet.remove("apple");
C) mySet.pop("apple");
D) mySet.delete("apple");
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
44
Which of the following statements about sets is correct?
A) Inserting and removing elements that have already been located is faster with a list than with a set.
B) A set allows duplicate values.
C) You can add an element to a specific position within a set.
D) A set is a collection of unique elements organized for efficiency.
A) Inserting and removing elements that have already been located is faster with a list than with a set.
B) A set allows duplicate values.
C) You can add an element to a specific position within a set.
D) A set is a collection of unique elements organized for efficiency.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
45
In a linked list data structure, when does the reference to the first node need to be updated?
I inserting into an empty list
II deleting from a list with one node
III deleting an inner node
A) I
B) II
C) I and II
D) III
I inserting into an empty list
II deleting from a list with one node
III deleting an inner node
A) I
B) II
C) I and II
D) III
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
46
Which of the following statements about manipulating objects in a map is NOT correct?
A) Use the add method to add a new element to the map.
B) Use the get method to retrieve a value from the map.
C) Use the keyset method to get the set of keys for the map.
D) Use the remove method to remove a value from the map.
A) Use the add method to add a new element to the map.
B) Use the get method to retrieve a value from the map.
C) Use the keyset method to get the set of keys for the map.
D) Use the remove method to remove a value from the map.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
47
The nodes of a(n) ____ linked list class store two links: one to the next element and one to the previous one.
A) array
B) singly
C) doubly
D) randomly
A) array
B) singly
C) doubly
D) randomly
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
48
Consider the following code snippet: LinkedList myLList = new LinkedList();
MyLList.add("Mary");
MyLList.add("John");
MyLList.add("Sue");
ListIterator iterator = myLList.listIterator();
Iterator.next();
Iterator.next();
Iterator.add("Robert");
Iterator.previous();
Iterator.previous();
Iterator.remove();
System.out.println(myLList);
What will be printed when this code is executed?
A) [Mary, John, Robert, Sue]
B) [Mary, John, Sue]
C) [Mary, Robert, Sue]
D) [John, Robert, Sue]
MyLList.add("Mary");
MyLList.add("John");
MyLList.add("Sue");
ListIterator
Iterator.next();
Iterator.next();
Iterator.add("Robert");
Iterator.previous();
Iterator.previous();
Iterator.remove();
System.out.println(myLList);
What will be printed when this code is executed?
A) [Mary, John, Robert, Sue]
B) [Mary, John, Sue]
C) [Mary, Robert, Sue]
D) [John, Robert, Sue]
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
49
Assume you are using a doubly-linked list data structure with many nodes. What is the minimum number of node references that are required to be modified to remove a node from the middle of the list? Consider the neighboring nodes.
A) 1
B) 2
C) 3
D) 4
A) 1
B) 2
C) 3
D) 4
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
50
To create a TreeSet for a class of objects, the object class must ____.
A) create an iterator.
B) implement the Comparable interface.
C) implement the Set interface.
D) create a Comparator object.
A) create an iterator.
B) implement the Comparable interface.
C) implement the Set interface.
D) create a Comparator object.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
51
Which of the following statements about the TreeSet class is NOT correct?
A) Elements are stored in sorted order.
B) Elements are arranged in linear fashion.
C) Elements are stored in nodes.
D) To use a TreeSet, it must be possible to compare the elements.
A) Elements are stored in sorted order.
B) Elements are arranged in linear fashion.
C) Elements are stored in nodes.
D) To use a TreeSet, it must be possible to compare the elements.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
52
Complete the following code snippet, which is intended to determine if a specific value in a variable named targetWord appears in a set of String values named mySet: for (String aWord : mySet)
{
_______________________
{
System.out.println ("The word " + targetWord + " was found.");
}
)
A) if (mySet.equalsIgnoreCase(targetWord))
B) if (mySet == targetWord)
C) if (mySet.contains(targetWord))
D) if (mySet.get(targetWord))
{
_______________________
{
System.out.println ("The word " + targetWord + " was found.");
}
)
A) if (mySet.equalsIgnoreCase(targetWord))
B) if (mySet == targetWord)
C) if (mySet.contains(targetWord))
D) if (mySet.get(targetWord))
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
53
Assume that you have declared a set named mySet to hold String elements. Which of the following statements will correctly insert an element into mySet?
A) mySet.insert("apple");
B) mySet.put(apple");
C) mySet.push("apple");
D) mySet.add("apple");
A) mySet.insert("apple");
B) mySet.put(apple");
C) mySet.push("apple");
D) mySet.add("apple");
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
54
Select an appropriate declaration to complete the following code segment, which is designed to read strings from standard input and display them in increasing alphabetical order, excluding any duplicates. _________________________________________
Scanner input = new Scanner(System.in);
While (input.hasNext())
{
Words.add(input.next());
}
System.out.print(words);
A) LinkedList words = new LinkedList();
B) Set words = new Set();
C) Set words = new HashSet();
D) Set words = new TreeSet();
Scanner input = new Scanner(System.in);
While (input.hasNext())
{
Words.add(input.next());
}
System.out.print(words);
A) LinkedList
B) Set
C) Set
D) Set
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
55
Which of the following statements about manipulating objects in a set is correct?
A) If you try to add an element that already exists, an exception will occur.
B) A set iterator visits elements in the order in which they were added to the set.
C) You can add an element at the position indicated by an iterator.
D) You can remove an element at the position indicated by an iterator.
A) If you try to add an element that already exists, an exception will occur.
B) A set iterator visits elements in the order in which they were added to the set.
C) You can add an element at the position indicated by an iterator.
D) You can remove an element at the position indicated by an iterator.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
56
Complete the following code, which is intended to print out all key/value pairs in a map named myMap that contains String data for student IDs and names: Map myMap = new HashMap();
) . .
Set mapKeySet = myMap.keySet();
For (String aKey : mapKeySet)
{
___________________________;
System.out.println("ID: " + aKey + "->" + name);
}
A) String name = myMap.get(aKey);
B) String name = myMap.next(aKey);
C) String name = MapKeySet.get(aKey);
D) String name = MapKeySet.next(aKey);
) . .
Set
For (String aKey : mapKeySet)
{
___________________________;
System.out.println("ID: " + aKey + "->" + name);
}
A) String name = myMap.get(aKey);
B) String name = myMap.next(aKey);
C) String name = MapKeySet.get(aKey);
D) String name = MapKeySet.next(aKey);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
57
Select an appropriate expression to complete the following method, which is designed to return the number of elements in the parameter array numbers. If a value appears more than once, it should be counted exactly once. public static int countElementsOnce(int[] numbers)
{
Set values = new HashSet();
For (int num: numbers)
{
Values.add(num);
}
______________________
}
A) return numbers.length;
B) return values.length();
C) return values.size();
D) return numbers.length - values.size();
{
Set
For (int num: numbers)
{
Values.add(num);
}
______________________
}
A) return numbers.length;
B) return values.length();
C) return values.size();
D) return numbers.length - values.size();
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
58
Which of the following statements about hash tables is NOT correct?
A) Elements are grouped into smaller collections that share the same characteristic.
B) You can form hash tables holding objects of type String.
C) You can add an element to a specific position within a hash table.
D) The value used to locate an element in a hash table is called a hash code.
A) Elements are grouped into smaller collections that share the same characteristic.
B) You can form hash tables holding objects of type String.
C) You can add an element to a specific position within a hash table.
D) The value used to locate an element in a hash table is called a hash code.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
59
Which of the following statements about data structures is correct?
A) Inserting and removing elements that have already been located is faster with a list than with a set.
B) Accessing elements in a linked list in a random fashion is efficient.
C) Adding and removing already-located elements in the middle of a linked list is efficient.
D) A set is an ordered collection of unique elements.
A) Inserting and removing elements that have already been located is faster with a list than with a set.
B) Accessing elements in a linked list in a random fashion is efficient.
C) Adding and removing already-located elements in the middle of a linked list is efficient.
D) A set is an ordered collection of unique elements.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
60
Complete the following code, which is intended to print out all key/value pairs in a map named myMap that contains String data for student IDs and names: Map myMap = new HashMap();
) . .
_______________________________
For (String aKey : mapKeySet)
{
String name = myMap.get(aKey);
System.out.println("ID: " + aKey + "->" + name);
}
A) Map mapKeySet = myMap.keySet();
B) Set mapKeySet = myMap.keySet();
C) Set mapKeySet = myMap.getKeySet();
D) Set mapKeySet = myMap.keySet();
) . .
_______________________________
For (String aKey : mapKeySet)
{
String name = myMap.get(aKey);
System.out.println("ID: " + aKey + "->" + name);
}
A) Map
B) Set
C) Set
D) Set
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
61
You need to access values by an integer position. Which collection type should you use?
A) Map
B) Hashtable
C) ArrayList
D) Queue
A) Map
B) Hashtable
C) ArrayList
D) Queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
62
What operation is least efficient in a LinkedList?
A) Adding an element in a position that has already been located.
B) Linear traversal step.
C) Removing an element when the element's position has already been located.
D) Random access of an element.
A) Adding an element in a position that has already been located.
B) Linear traversal step.
C) Removing an element when the element's position has already been located.
D) Random access of an element.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
63
You need to write a program to simulate the effect of adding an additional cashier in a supermarket to reduce the length of time customers must wait to check out. Which data structure would be most appropriate to simulate the waiting customers?
A) map
B) stack
C) queue
D) linked list
A) map
B) stack
C) queue
D) linked list
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
64
You need to access values in objects by a key that is not part of the object. Which collection type should you use?
A) Map
B) Hashtable
C) ArrayList
D) Queue
A) Map
B) Hashtable
C) ArrayList
D) Queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
65
Which data structure would best be used for storing a set of numbers and sorting them in ascending order?
A) queue
B) stack
C) list
D) array
A) queue
B) stack
C) list
D) array
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
66
Assume that you have declared a map named myMap to hold String elements with Integer keys. Which of the following statements will correctly insert an element into myMap?
A) myMap.insert(3, "apple");
B) myMap.put(3, "apple");
C) myMap.push(3, "apple");
D) myMap.add(3, "apple");
A) myMap.insert(3, "apple");
B) myMap.put(3, "apple");
C) myMap.push(3, "apple");
D) myMap.add(3, "apple");
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
67
Consider the following code snippet: Map scores;
You expect to retrieve elements randomly by key, and want fastest retrieval times. Which of the following statements will create a structure to support this?
A) scores = new HashMap;
B) scores = new TreeMap;
C) scores = new Map;
D) scores = new TreeSet;
You expect to retrieve elements randomly by key, and want fastest retrieval times. Which of the following statements will create a structure to support this?
A) scores = new HashMap
B) scores = new TreeMap
C) scores = new Map
D) scores = new TreeSet
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
68
You need to access values in the order in which they were added (first in, first out), and not randomly. Which collection type should you use?
A) Map
B) Hashtable
C) Stack
D) Queue
A) Map
B) Hashtable
C) Stack
D) Queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
69
You have decided to store objects of a class in a TreeSet structure. Which of the following statements is correct?
A) If the object class implements the Comparable interface, and the sort order in the compare method is acceptable, you do not have to do anything else.
B) If the object class implements the Comparable interface, and the sort order in the compareTo method is acceptable, you do not have to do anything else.
C) If the object class implements the Comparable interface, and the sort order in the compare method is acceptable, you must create a comparator object.
D) If the object class implements the Comparable interface, and the sort order in the compareTo method is acceptable, you must create a comparator object.
A) If the object class implements the Comparable interface, and the sort order in the compare method is acceptable, you do not have to do anything else.
B) If the object class implements the Comparable interface, and the sort order in the compareTo method is acceptable, you do not have to do anything else.
C) If the object class implements the Comparable interface, and the sort order in the compare method is acceptable, you must create a comparator object.
D) If the object class implements the Comparable interface, and the sort order in the compareTo method is acceptable, you must create a comparator object.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
70
Assume that you have declared a map named myMap to hold String elements with Integer keys. Which of the following statements will correctly retrieve the value of an element from myMap by using its key?
A) myMap.get("apple");
B) myMap.peek("apple");
C) myMap.get(3);
D) myMap.peek(3);
A) myMap.get("apple");
B) myMap.peek("apple");
C) myMap.get(3);
D) myMap.peek(3);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
71
You want to enumerate all of the keys in a map named myMap whose keys are type String. Which of the following statements will allow you to do this?
A) Set keySet = myMap.keySet();
For (String key : keySet) {. . . }
B) Set keySet = myMap.getKeys();
For (String key : keySet) {. . . }
C) Set keySet = myMap.keys();
For (String key : keySet) {. . . }
D) Set keySet = myMap.getKeySet();
For (String key : keySet) {. . . }
A) Set
For (String key : keySet) {. . . }
B) Set
For (String key : keySet) {. . . }
C) Set
For (String key : keySet) {. . . }
D) Set
For (String key : keySet) {. . . }
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
72
You need to write a program to build and maintain an address book. Since the program must support the possibility of having duplicate names, which data structure would be most appropriate to model this situation?
A) map
B) stack
C) queue
D) linked list
A) map
B) stack
C) queue
D) linked list
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
73
You need to access values in the opposite order in which they were added (last in, first out), and not randomly. Which collection type should you use?
A) Map
B) Hashtable
C) Stack
D) Queue
A) Map
B) Hashtable
C) Stack
D) Queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
74
Which of the following statements about manipulating objects in a map is NOT correct?
A) If you attempt to retrieve a value with a key that is not associated with any value, you will receive a null result.
B) You cannot change the value of an existing association in the map; you must delete it and re-add it with the new values.
C) Use the get method to retrieve a value associated with a key in the map.
D) Use the put method to add an element to the map.
A) If you attempt to retrieve a value with a key that is not associated with any value, you will receive a null result.
B) You cannot change the value of an existing association in the map; you must delete it and re-add it with the new values.
C) Use the get method to retrieve a value associated with a key in the map.
D) Use the put method to add an element to the map.
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
75
Your program uses a Map structure to store a number of user ids and corresponding email addresses. Although each user must have a unique id, two or more users can share the same email address. Select an appropriate expression to complete the method below, which adds a new id and email address to the map only if the id is not already in use. If the id is already in use, an error message is printed. public static void addUserID(Map users, String id, String email)
{
String currentEmail = users.get(id);
If (___________________)
{
Users.put(id, email);
}
Else
{
System.out.println(id + " is already in use.");
}
}
A) currentEmail.equals(email)
B) !currentEmail.equals(email)
C) currentEmail == null
D) currentEmail != null
{
String currentEmail = users.get(id);
If (___________________)
{
Users.put(id, email);
}
Else
{
System.out.println(id + " is already in use.");
}
}
A) currentEmail.equals(email)
B) !currentEmail.equals(email)
C) currentEmail == null
D) currentEmail != null
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
76
You need to access values using a key, and the keys must be sorted. Which collection type should you use?
A) TreeMap
B) ArrayList
C) HashMap
D) Queue
A) TreeMap
B) ArrayList
C) HashMap
D) Queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
77
You need to access values by their position. Which collection type should you use?
A) TreeSet
B) ArrayList
C) Stack
D) Queue
A) TreeSet
B) ArrayList
C) Stack
D) Queue
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
78
Consider the following code snippet:
Map scores;
If you need to visit the keys in sorted order, which of the following statements will create a structure to support this?
a) scores = new HashMap;
b) scores = new TreeMap;
c) scores = new Map;
d) scores = new HashTable;
Map
If you need to visit the keys in sorted order, which of the following statements will create a structure to support this?
a) scores = new HashMap
b) scores = new TreeMap
c) scores = new Map
d) scores = new HashTable
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
79
Assume that you have declared a map named myMap to hold String elements with Integer keys. Which of the following statements will correctly remove an element from myMap?
A) myMap.get(3);
B) myMap.remove(3);
C) myMap.pop(3);
D) myMap.delete(3);
A) myMap.get(3);
B) myMap.remove(3);
C) myMap.pop(3);
D) myMap.delete(3);
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck
80
Which of the following algorithms would be efficiently executed on an ArrayList?
A) add 1 element to the middle of a list with n elements
B) add n / 2 elements to a list with n / 2 elements
C) remove first n / 2 elements from a list of n elements
D) read n / 2 elements in random order from a list of n elements
A) add 1 element to the middle of a list with n elements
B) add n / 2 elements to a list with n / 2 elements
C) remove first n / 2 elements from a list of n elements
D) read n / 2 elements in random order from a list of n elements
Unlock Deck
Unlock for access to all 110 flashcards in this deck.
Unlock Deck
k this deck