CS 109 Chapeter 9 Stacks & Queues for Data Structures.

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Which of the following is a practical example of a doubly linked list? 1. A game in which the player runs forward. 2. A browser cookie file. 3. A quest in a game that lets users retry stages. 4. A first-in-first out scheduling system.

A quest in a game that lets users retry stages. A quest in a game that lets users retry stages. Explanation The back/forward functionality is best served by a doubly linked list. You can't go past the last step, nor can you proceed before the first. However, you can go back and forth within the quest itself.

Which of the following correctly checks to see if the current node is the head of a doubly linked list? 1. Node myNode = new Node(element); if(myNode.prev == null) { } 2. Node myNode = new Node(element); if(myNode == null) { } 3. Node myNode = new Node(element); if(myNode.next == tail) { } 4. Node myNode = new Node(element); if(myNode.next == null) { }

Node myNode = new Node(element); if(myNode.prev == null) { } Explanation If the previous pointer is null, it means that the current node is the head of the list.

When creating a stack data type in Java, which methods are most appropriate for the interface to the Stack? 1. isEmpty, pop, size 2. Push, poke, peek, size, isEmpty 3. Push, pop, peek, size, isEmpty 4. Push, peek, size, isEmpty

Push, pop, peek, size, isEmpty. Push, pop, peek, size, isEmpty. Push, pop, peek, size, isEmpty. Push, pop, peek, size, isEmpty. Explanation We must be able to add (push) and remove (pop) elements from the stack. We should also be able to view the top option (peek) without moving it, check the size, and determine if it is an empty stack.

Which of the following Java statements is correct with a queue name of 'myQueue' and the data of type integer? 1. add(myQueue.319); 2. myQueue.add(319); 3. myQueue.add('319'); 4. add(myQueue, 319);

myQueue.add(319); myQueue.add(319); myQueue.add(319); myQueue.add(319); myQueue.add(319); myQueue.add(319);

Which method retrieves but does not remove the element at the head of the deque? 1. pollFirst 2. remove 3. peek 4. removeFirst

peek. peek. peek. peek. peek. peek. peek. peek. peek. Explanation The peek retrieves the head item but does not remove it.

Which Queue Interface method retrieves the front element of a queue without removing it? poll look peek offer

peek. peek. peek. peek. peek. peek. peek. peek. peek. peek. peek. peek. peek. peek. peek. peek. peek. peek. Explanation The peek method retrieves the element from the front of the queue but does not remove it.

Which Queue Interface method retrieves and removes the front element of a queue? 1. poll 2. peek 3. look 4. offer

poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. poll. Explanation The poll method retrieves the element from the front of the queue and also removes it.

Which construct is used by regular queues? 1. last-in, first-out 2. last-in, last-out 3. first-in, first-out 4. first-in, last-out

first-in, first-out. first-in, first-out. first-in, first-out. first-in, first-out. first-in, first-out. first-in, first-out. Explanation Regular queues have a head and a tail and use the first-in, first-out construct.

Which of the following correctly appends a new value specifically to the tail of the following linked list: LinkedList l = new LinkedList(); l.add("M"); l.add("A"); l.add("R"); 1. l.addFirst("S"); 2. l.addLast("S"); 3. l.remove("S"); 4. l.replace("S");

l.addLast("S");. l.addLast("S");. l.addLast("S");. l.addLast("S"); Explanation The addLast method will add to the end (tail) of the linked list.

Which construct is used by stacks? 1. first-in, first-out 2. first-in, last-out 3. last-in, first-out 4. last-in, last-out

last-in, first-out. last-in, first-out. last-in, first-out Explanation Stacks, an abstract data type, use the last-in, first-out construct.

In the following linked list (named m in your code), you need to change the Z to an S. Which code example accomplishes this? [M, A, R, Z]; 1. m.addLast("S"); 2. m.set(3, "S"); 3. m.add("S"); 4. m.addFirst("S");

m.set(3, "S");. m.set(3, "S");. m.set(3, "S");. m.set(3, "S");. Explanation Java starts counting at zero, so the fourth letter is index 3; use the set method to set the fourth node to an S.

Which of the following Java statements best depicts the instantiation of the first element within a stack? 1. public int push(); 2. private Node tail; 3. private Node top; 4. public int pop;

private Node top; private Node top; private Node top; Explanation We use a class (usually called Node) to represent an element in our linked list. Since the head of the list is the one we can use in a stack, we can name it top.

What could you change to make the following code not result in an error? interface A { public int one(); public int two(); public int three(); } public class B implements C { public int one() { return 1; public int two() { return 2; public int three() { return 3; } } 1. Add an abstract class. 2. Add the ''implements'' keyword to the class method declarations. 3. Change ''implements C'' to ''implements A'' 4. Move the method definitions from the class to the interface.

Change ''implements C'' to ''implements A''. Change ''implements C'' to ''implements A'' Explanation In this example there is no C to implement.

Which type of queue can be used as a queue or a stack? 1. Double-ended queue 2. Regular queue 3. Ordered queue 4. Priority queue

Double-ended queue. Double-ended queue. Double-ended queue. Double-ended queue. Double-ended queue. Explanation Double-ended queues can be used as either a queue or a stack.

When you add a new element to the tail of a circularly linked list, what is true of the current tail? 1. It has no connections 2. Its value is set to 0 3. It connects to the head 4. It is the first item

It connects to the head. It connects to the head. It connects to the head. It connects to the head. Explanation tail.next = head; This is the code that tells Java that the new tail's next element is now the head, so it keeps the circular list in sync.

Stack operation is based on what approach? 1. FOLI 2. LOFI 3. LIFO 4. FIFO

LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. LIFO. Explanation The operations in Stack data structure is based on last-in-first-out, and it is predicated on the fact that there is only one entry point which also serves as exit point and called top.

Which statement is true? 1. The first-in, first-out structure is mandatory for both regular and priority queues. 2. The first-in, first-out structure is used for regular and priority queues. 3. The first-in, first out structure is mandatory for regular queues, and optional for priority queues. 4. Regular queues use the first-in, first-out structure while priority queues remove elements based on priority.

Regular queues use the first-in, first-out structure while priority queues remove elements based on priority. Regular queues use the first-in, first-out structure while priority queues remove elements based on priority. Regular queues use the first-in, first-out structure while priority queues remove elements based on priority.

Which is true about an interface definition? 1. There are no variables. 2. It cannot be public. 3. The method definitions do not have parameters. 4. The methods have no bodies.

The methods have no bodies. The methods have no bodies. The methods have no bodies. The methods have no bodies. Explanation An interface is like a blueprint. The method's parameters and return type are defined, but there is no method body.

When using a linked list to represent a stack, which element(s) do you have access to within the stack? The bottom (head) The top (tail) All nodes in the stack All nodes before the top (tail)

The top (tail). The top (tail). The top (tail). The top (tail) The top (tail). The top (tail). The top (tail). The top (tail) Explanation A stack is last-in-first-out, meaning you only have access to the top of the stack and therefore have to remove items from the top if you wanted to get to any other nodes.

Which of the following statements is true about an unsorted list? 1. They have to be used for priority queue implementation. 2. Unsorted lists have no organized order. 3. They are the mandatory data structure for priority queues. 4. They cannot be used for priority queue implementation.

Unsorted lists have no organized order. Unsorted lists have no organized order. Unsorted lists have no organized order. Unsorted lists have no organized order.


Set pelajaran terkait

chapter 10 motivation, personality, and emotions

View Set

Psych Ch 16, Psychology Chapter 15, Psychology

View Set

Intermediate Accounting Midterm 2

View Set

CH. 10 PLANETARY ATMOSPHERES... HAHA

View Set

Chapter 7: Long-Term Memory - Structure (Encoding, Retrieval, and Consolidation)

View Set

Chapter 18 - Formation of Sales and Lease Contracts

View Set

Chapter 12: The Future of Data Systems

View Set

Mrs. Long AP Psych Semester 1 Final- Multiple Choice Q&A

View Set