Sample Data Structures Questions
Chapter 7
Queues

Data Structures and Other Objects Using Java Third Edition
by Michael Main


The Purpose of These Questions

These are typical exam questions from Chapter 7 of the textbook. These exact questions might not be on your exam, but if you research and find the right answers to these questions, that should be good preparation for a real exam. (It's also possible that some of this material was not covered in your class.) There are 5 short answer questions and 14 multiple choice questions in this file (which was last updated on Nov. 1, 2007 by Dr. O.).

Short Answers

    Short Answers
    Sections 7.1 - 7.2
    Queues and Their
    Applications
  1. I am going to execute this code with THREE adds and ONE remove:
    
       ArrayQueue<Integer> q = new ArrayQueue<Integer>( );
       q.add(1);
       q.add(2);
       q.add(3);
       System.out.println(q.remove( ));
    
    Suppose that q is represented by a circular array. Draw the state of these private instance variables of q after the above code:
    
               _______            __________________________________
         front|       |      data|      |      |      |      |      |
              |_______|          |______|______|______|______|______|
                                   [0]    [1]    [2]    [3]    [4]
    

  2. I am going to execute this code with THREE adds and ONE removet:
    
       ArrayQueue<Integer. q = new ArrayQueue<Integer>( );
       q.add(1);
       q.add(2);
       q.add(3);
       System.out.println(q.getFront( ));
    
    Suppose that q is represented by a linked list. Draw the state of the private instance variables of q after the above code:
    
                   _______      
             front|       |  
                  |_______|     
                   _______      
              rear|       |  
                  |_______|     
    

  3. Describe why it is a bad idea to implement a linked list version a queue which uses the head of the list as the rear of the queue.

Multiple Choice

    Multiple Choice
    Sections 7.1-7.2
    Queues and Their
    Applications
  1. One difference between a queue and a stack is:

  2. If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order), and then removed one at a time, in what order will they be removed?

  3. Which of the following expressions evaluates to true with approximate probability equal to P? (P is double and 0 <= P <= 1).

    Multiple Choice
    Section 7.3
    Implementations of
    the Queue ADT

  4. Suppose we have a circular array implementation of the queue class, with ten items in the queue stored at data[2] through data[11]. The current capacity is 42. Where does the insert method place the new entry in the array?

  5. Consider the implementation of the Queue using a circular array. What goes wrong if we try to keep all the items at the front of a partially-filled array (so that data[0] is always the front).

  6. In the linked list implementation of the queue class, where does the insert method place the new entry on the linked list?

  7. In the circular array version of the Queue class, which operations require linear time for their worst-case behavior?

  8. In the linked-list version of the Queue class, which operations require linear time for their worst-case behavior?

  9. If data is a circular array of CAPACITY elements, and rear is an index into that array, what is the formula for the index after rear?

  10. I have implemented the queue with a circular array, keeping track of front, rear, and manyItems (the number of items in the array). Suppose front is zero, and rear is one less than the current capacity. What can you tell me about manyItems?

  11. I have implemented the queue with a linked list, keeping track of a front node and a rear node with two reference variables. Which of these reference variables will change during an insertion into a NONEMPTY queue?

  12. I have implemented the queue with a linked list, keeping track of a front node and a rear node with two reference variables. Which of these reference variables will change during an insertion into an EMPTY queue?

    Multiple Choice
    Section 7.4
    Priority Queues

  13. Suppose getFront is called on a priority queue that has exactly two entries with equal priority. How is the return value of getFront selected?

  14. An array of queues can be used to implement a priority queue, with each possible priority corresponding to its own element in the array. When is this implementation not feasible?