Ch. 15 Quiz

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Which method is NOT part of the ListIterator interface?

delete

Which method is NOT part of the ListIterator interface?

hasMore

To create a TreeSet for a class of objects, the object class must ____.

implement the Comparable interface.

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<String> players = new LinkedList<>(); // code to add elements to the linked list if ______________________________________ { System.out.print(name + " is the first player on the list."); }

(players.getFirst().equals(name))

Which of the following statements about linked lists is correct?

Linked lists should be used when you know the correct position and need to insert and remove elements efficiently.

Which Java package contains the LinkedList class?

java.util

When using a list iterator, on which condition will the NoSuchElementException be thrown?

Calling next when you are past the end of the list.

When using a list iterator, on which condition will the IllegalStateException be thrown?

Calling remove after calling add.

What operation is least efficient in a LinkedList?

Random access of an element.

You need to access values using a key, and the keys must be sorted. Which collection type should you use?

TreeMap

Which of the following statements about hash tables is NOT correct?

You can add an element to a specific position within a hash table.

Consider the following code snippet: PriorityQueue<String> stringQueue = new PriorityQueue<>(); stringQueue.add("ab"); stringQueue.add("abc"); stringQueue.add("a"); while (stringQueue.size() > 0) { System.out.print(stringQueue.remove() + ","); } What output will be produced when this code is executed?

a,ab,abc,

Consider the following code snippet: Stack<String> stringStack = new Stack<>(); stringStack.push("ab"); stringStack.push("abc"); stringStack.push("a"); while (!stringStack.empty()) { System.out.print(stringStack.pop() + ","); } What output will be produced when this code is executed?

a,abc,ab,

A ____ 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.

linked list

You use a(n) ____ to access elements inside a linked list.

list iterator

Assume you have created a linked list named myList that currently holds some number of String objects. Which of the following statements correctly removes an element from the end of myList?

myList.removeLast();

Assume that you have declared a map named myMap to hold String values with Integer keys. Which of the following statements will correctly retrieve the value associated with a key from myMap?

myMap.get(3);

Rather than storing values in an array, a linked list uses a sequence of ____.

nodes

A collection that allows items to be added only at one end and removed only at the other end is called a ____.

queue

A linear search only requires ____ access.

sequential

What type of access does a LinkedList provide for its elements?

sequential

Which of the following statements about sets is correct?

A set is a collection of unique elements organized for efficiency.

When using a list iterator, on which condition will the IllegalStateException be thrown?

Calling remove after calling remove.

What is included in a linked list node? I a reference to its neighboring nodes II an array reference III a data element

I and III only

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

I only

Which operations from the list data structure could be used to implement the push and pop operations of a stack data structure? I addLast II addFirst III removeFirst

II and III only

Which of the following algorithms would be efficiently executed using a LinkedList?

Remove first n/ 2 elements from a list of n elements.

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<String> theList, String name) { int number = 0; Iterator<String> iter = theList.iterator(); while (______________________) { if (iter.next().equals(name)) { number++; } } return number; }

iter.hasNext()

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<String> theList) { ListIterator<String> iterator = theList.listIterator(); while (iterator.hasNext()) { if (iterator.next().equals("hello")) { _____________________________ } } }

iterator.set("goodbye");

Consider the following code snippet: Map<String, Integer> scores; If you need to visit the keys in sorted order, which of the following statements will create a structure to support this?

scores = new TreeMap<>;

A list is a collection that ____.

should be used when you need to remember the order of elements in the collection.

You need a data structure in your program for finding a path out of a maze using backtracking. Which data structure would be most appropriate to model this situation?

stack

Using the merge method of the Map interface, which statement correctly updates the salesTotalByDept map of type Map<String,Integer> to update the sales total for a dept by an integer sales value?

salesTotalByDept.merge(dept, sales, (d,s) -> s + sales);

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?

mySet.add("apple");

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?

queue

The term ____ is used in computer science to describe an access pattern in which the elements are accessed in arbitrary order.

random access

Which of the following algorithms would be efficiently executed on an ArrayList?

read n / 2 elements in random order from a list of n elements

A collection without an intrinsic order is called a ____.

set

Which of the following statements about manipulating objects in a set is correct?

A set iterator visits elements in the order in which the set implementation keeps them.

Consider the code snippet shown below. Assume that employeeNames is an instance of type LinkedList<String>. for (String name : employeeNames) { // Do something with name here } Which element(s) of employeeNames does this loop process?

all elements

Assume that you have declared a map named myMap to hold String values with Integer keys. Which of the following statements will correctly add an association into myMap?

myMap.put(3, "apple");

Assume that you have declared a map named myMap to hold String values with Integer keys. Which of the following statements will correctly delete an association from myMap?

myMap.remove(3);

What can a generic class be parameterized for?

type

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<String> players = new ArrayList<>(); // code to add elements to the collection here if ______________________________________ { System.out.print(name + " is one of the players in the collection."); }

(players.contains(name))

Suppose we create a deque (double-ended queue) data structure. It is basically a queue, with its addLast and removeFirst operations, but we also add the addFirst and removeLast operations. Which of the following is best modeled by the deque data structure?

A computer keyboard typing buffer.

A collection that remembers the order of items, and allows items to be added and removed only at one end is called a ____.

stack

Using the merge method of the Map interface, which statement correctly updates the wordCount map of type Map<String,Integer> to count all of the occurrences of a word in a file?

wordCount.merge(word, 1, (k,v) -> v + 1);

Which of the following statements about hash functions is NOT correct?

A hash function produces a unique integer-valued hash code value for each distinct object.

Which of the following statements about the TreeSet class is NOT correct?

Elements are arranged in linear fashion.

Which of the following statements about a priority queue structure is correct?

Elements must be removed in priority order.

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

I and II only

You need to access values in objects by a key that is not part of the object. Which collection type should you use?

Map

Suppose we have two String objects and treat the characters in each string from beginning to end in the following way: With one string, we push each character on a stack. With the other string, we add each character to a queue. After processing both strings, we then pop one character from the stack and remove one character from the queue, and compare the pair of characters to each other. We do this until the stack and the queue are both empty. What does it mean if all the character pairs match?

One string is the reverse of the other.

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?

The current third and fourth nodes.

You intend to use a hash set with your own object class. Which of the following statements is correct?

You can use the hash method of the Objects class to create your own hashCode method by combining the hash codes for the instance variables.

Consider the code snippet shown below: Stack<String> words1 = new Stack<>(); Stack<String> words2 = new Stack<>(); words1.push("abc"); words1.push("def"); words1.push("ghi"); while (!words1.empty()) { words2.push(words1.pop()); } while (!words2.empty()) { System.out.print(words2.pop()); } What will be printed when this code is executed?

abcdefghi

Consider the following code snippet: LinkedList<String> 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?

defghiabc

Which data structure would best be used for keeping track of groceries to be purchased at the food market?

list

Assume that you have declared a queue named myQueue to hold String elements. Which of the following statements will correctly insert an element into myQueue?

myQueue.add("apple");

Assume that you have declared a stack named myStack to hold String elements. Which of the following statements will correctly retrieve the top element from myStack without removing it?

myStack.peek();

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<Integer> values = new HashSet<>(); for (int num: numbers) { values.add(num); } ______________________ }

return values.size();

An Undo feature in a word processor program that allows you to reverse a previously completed command is probably implemented using which structure type?

stack

You need a data structure in your program for balancing parentheses in an expression. Which data structure would be most appropriate to model this situation?

stack

You need a data structure in your program for evaluating algebraic expressions. Which data structure would be most appropriate to model this situation?

stack

A binary search requires ____ access.

random

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?

Set<String> keySet = myMap.keySet(); for (String key : keySet) {. . . }

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<String, String> myMap = new HashMap<>(); . . . _______________________________ for (String aKey : mapKeySet) { String name = myMap.get(aKey); System.out.println("ID: " + aKey + "->" + name); }

Set<String> mapKeySet = myMap.keySet();

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);

Set<String> words = new TreeSet<>();

When using the add method of the ListIterator to add an element to a linked list, which of the following statements is correct?

The new element is inserted before the iterator position, and a subsequent call to next would be unaffected.

Consider the following code snippet: Map<String, Integer> 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?

scores = new HashMap<>;

Suppose you push integer elements 1,2,3,4 onto a stack in that order. Then pop an element off the stack and add that element to a queue. You repeat that process three more times. In what order will you remove the elements from the queue?

4,3,2,1

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.

2

Which of the following statements about stacks is correct?

A stack implements last-in, first-out retrieval.

You need to access values by an integer position. Which collection type should you use?

ArrayList

Which operations from the list data structure could be used to implement the push and pop operations of a stack data structure?

II and III only

The ArrayList class implements the ____ interface.

List

Select an appropriate expression to complete the method below, which is designed to print the element at the bottom of a Stack collection. The contents of the original stack are restored before the method terminates. It is safe to assume that the original stack contains at least one element. public static void printBottom(Stack<String> theStack) { Stack<String> anotherStack = new Stack<>(); while (!theStack.empty()) { anotherStack.push(theStack.pop()); } ____________________________ while (!anotherStack.empty()) { theStack.push(anotherStack.pop()); } }

System.out.println(anotherStack.peek());

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?

Queue

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?

Stack

Which of the following correctly declares a stack that will hold String elements?

Stack<String> s = new Stack<>();

Which of the following statements about manipulating objects in a map is NOT correct?

Use the add method to add a new element to the map.

Which of the following statements about manipulating objects in a set is correct?

You can remove an element at the position indicated by an iterator.

Which of the following statements about manipulating objects in a map is NOT correct?

You cannot change the value of an existing association in the map; you must delete it and re-add it with the new values.

Consider the following code snippet: LinkedList<String> myLList = new LinkedList<>(); myLList.add("Mary"); myLList.add("John"); myLList.add("Sue"); ListIterator<String> iterator = myLList.listIterator(); iterator.next(); iterator.next(); iterator.add("Robert"); iterator.previous(); iterator.previous(); iterator.remove(); System.out.println(myLList);

[Mary, Robert, Sue]

Consider the following code snippet: Queue<String> stringQueue = new LinkedList<>(); stringQueue.add("ab"); stringQueue.add("abc"); stringQueue.add("a"); while (stringQueue.size() > 0) { System.out.print(stringQueue.remove() + ","); } What output will be produced when this code is executed?

ab,abc,a,

Which data structure would best be used for storing a set of numbers and sorting them in ascending order?

array

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. Assuming that all entries in the map have valid email addresses, 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<String, String> users, String id, String email) { String currentEmail = users.get(id); if (___________________) { users.put(id, email); } else { System.out.println(id + " is already in use."); } }

currentEmail == null

The nodes of a(n) ____ linked list class store two links: one to the next element and one to the previous one.

doubly

A collection that allows speedy insertion and removal of already-located elements in the middle of it is called a ____.

linked list

Assume that you have declared a queue named myQueue to hold String elements. Which of the following statements will correctly delete an element from myQueue?

myQueue.remove();

Assume that you have declared a set named mySet to hold String elements. Which of the following statements will correctly delete an element from mySet?

mySet.remove("apple");

Assume that you have declared a stack named myStack to hold String elements. Which of the following statements will correctly remove an element from myStack?

myStack.pop();

Assume that you have declared a stack named myStack to hold String elements. Which of the following statements will correctly add an element to myStack?

myStack.push("apple");

Assuming that names is a Queue of String objects, select a statement to complete the code segment below. The code is designed to remove the last element from the queue, which is guaranteed to have at least one element. Queue<String> aQueue = new LinkedList<>(); while (names.size() > 1) { aQueue.add(names.remove()); } names.remove(); while (aQueue.size() > 0) { ____________________________ }

names.add(aQueue.remove());

Print jobs submitted to a printer would probably be stored in which type of data structure?

queue

You need to write a program to manage a waiting list of patrons at a restaurant. Which data structure would be most appropriate to model this situation?

queue

A queue is a collection that ____.

remembers the order of elements and allows elements to be inserted only at one end and removed only at the other end.

Select an appropriate expression to complete the following method, which is designed to return the sum of the two smallest values in the parameter array numbers. public static int sumTwoLowestElements(int[] numbers) { PriorityQueue<Integer> values = new PriorityQueue<>(); for (int num: numbers) { values.add(num); } ______________________ }

return values.remove() + values.remove();


Kaugnay na mga set ng pag-aaral

bacterial origin— cervicofacial actinomycosis

View Set

Chapter 1- A Framework for Maternal and Child Health Nursing

View Set

LACS 101 (College of Charleston) Final Exam- Chapter 9

View Set

Transposition of the Great Arteries

View Set

Chapter 8.2 The Louisiana Purchase

View Set