Quiz 06 sample questions

The quiz will have three questions, and cover material through the end of the stacks and queues topics.


In the first question, you will demonstrate your understanding of the behavior of the breadth-first search algorithm. There will be a problem similar to that from Homework 17 (reproduced here):

Suppose you have the following directed graph.

hw17-graph

(1 point) If you were to perform a breadth-first search, starting at S and trying to reach either of the two goal vertices (G1 or G2), in what order would the vertices be added to the frontier? Use the BFS algorithm we covered in class (findPath).

Assume that the neighbors of each vertex are returned in alphabetical order. For example, the only neighbors of S are A and B, in that order. But neither B nor A are neighbors of S.


In the second questions, you will show you understand the LIFO behavior of stacks and/or the FIFO behavior of queues (be prepared for either). For example, a question might state:

Suppose you have an empty stack, and you execute the following operations:

push(1);
push(2);
push(3);
peek()
pop();
push(1);
pop();
pop();

What are the contents of the stack? Be sure to clearly indicate the top and bottom of the stack.


The final question will ask you to write one of the operations that adds or removes an element from a stack or queue. Focus your study on the linked list implementations for this quiz, but be prepared to know either (linked list or array) in the future.

For example, a question might state:

Suppose we have a singly linked-list-based implementation of a Queue<E>, built of Node<E> elements that link to one another.

The Node<E> class has a public Node<E> next and a public E data instance variable.

The Queue<E> has head and tail instance variables, pointing to the first and last Nodes of the list (or null if the list is empty), as well as an int size.

In other words, it is the implementation we wrote in lecture.

Write the void add(E e) method.


You can check your answers here.