Self Quiz 3

Which statement is true?

Select one:

a. Queues require linked lists, but stacks do not.
b. Stacks require linked lists, but queues do not.
c. Queues use two ends of the structure; stacks use only one. 
d. Stacks use two ends of the structure, queues use only one.

The correct answer is: Queues use two ends of the structure; stacks use only one.





Question 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?

Select one:

a. ABCD
b. ABDC
c. DCAB
d. DCBA 

The correct answer is: DCBA



Question 3

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

Select one:

a. At the head.
b. At the tail. 
c. After all other entries that are greater than the new entry.
d. After all other entries that are smaller than the new entry.

The correct answer is: At the tail.




Question 4

Study the following three pieces of code. Comments have been removed intentionally.
Can you guess what each does?

(i)
public class ProcForInts {

   private int[] items = new int[10]; 
  
   private int top = 0; 
  
   /**
    * Procedure
    */
   public void push( int N ) {
       if (top == items.length) {
           int[] newArray = new int[ 2*items.length ];
           System.arraycopy(items, 0, newArray, 0, items.length);
           items = newArray;
       }
       items[top] = N; 
       top++;          
   }
  
   /**
    * Procedure
    */
   public int pop() {
       if ( top == 0 )
          throw new IllegalStateException("Can't…");
       int topItem = items[top - 1] 
       top--;   
       return topItem;
   }
  
   /**
    * Procedure
    */
   public boolean isEmpty() {
      return (top == 0);
   }

}


(ii)
public class ProcForInts {

   /**
    * Procedure
    */
   private static class Node {
      int item;
      Node next;
   }

   private Node head = null; 
  
   private Node tail = null;

   /**
    * Procedure
    */
   public void enqueue( int N ) {
      Node newTail = new Node(); 
      newTail.item = N;
      if (head == null) {
         head = newTail;
         tail = newTail;
      }
      else {
         tail.next = newTail;
         tail = newTail;
      }
   }
  
   /**
    * Procedure
    */
   public int dequeue() {
      if ( head == null)
          throw new IllegalStateException("Can't…");
      int firstItem = head.item;
      head = head.next; 
      if (head == null) {
            tail = null;
      }
      return firstItem;
   }
  
   /**
    * Procedure
    */
   boolean isEmpty() {
      return (head == null);
   }
  
}


(iii)
public class ProcForInts {

   private static class Node {
      int item;
      Node next;
   }
  
   private Node top; 
  
   /**
    * Procedure
    */
   public void push( int N ) {
      Node newTop;        
      newTop = new Node();
      newTop.item = N;    
      newTop.next = top;  
      top = newTop;       
   }
  
   /**
    * Procedure
    */
   public int pop() {
      if ( top == null )
         throw new IllegalStateException("Cannot…");
      int topItem = top.item; 
      top = top.next;         
      return topItem;
   }
  
   /**
    * Procedure
    */
   public boolean isEmpty() {
      return (top == null);
   }

}

Select one:

a. (i) is a linked list implementation of a stack; (ii) is an array implementation of a stack; (iii) is a queue
b. (i) is an array implementation of a stack; (ii) is a linked list implementation of a stack; (iii) is a queue
c. (i) is a queue; (ii) is a linked list implementation of a stack; (iii) is an array implementation of a stack
d. (i) is an array implementation of a queue; (ii) is a linked list implementation of a queue; (iii) is a stack
e. (i) is an array implementation of a stack; (ii) is a queue; (iii) is a linked list implementation of a stack 

The correct answer is: (i) is an array implementation of a stack; (ii) is a queue; (iii) is a linked list implementation of a stack



Question 5

Given the following code:

static void showOutput(int mark) {
    if (mark == 0) {
       System.out.print("*");
    }
    else {
       System.out.print("[");
       showOutput(mark - 1);
       System.out.print(",");
       showOutput(mark - 1);
       System.out.println("]");
    }
}
Can you determine what is produced by the following subroutine calls:
showOutput(0), showOutput(1), showOutput(2), and showOutput(3)?

a.
showOutput(0) outputs:   *
showOutput(1) outputs:   [*,*]
showOutput(2) outputs:   [[*,*],[*,*]]
showOutput(3) outputs:   [[[*,*],[*,*]],[[*,*],[*,*]]]

b.
showOutput(0) outputs:   [
showOutput(1) outputs:   *,*
showOutput(2) outputs:   [[],[]]
showOutput(3) outputs:   [[[*,*],[*,*]],[[*,*],[*,*]]]

Select one:
a. 
b.

The correct answer is: a.




Question 6

How many leaves does the tree below have?
       14
      /  \
     2    11
    / \   /  \
   1  3  10  30
           /    /           
         7  40

Select one:

a. 2
b. 4 
c. 6
d. 8
e. 9

The correct answer is: 4



Question 7

Consider the tree in the previous question. What is the value stored in the parent node of the node containing 30?

Select one:

a. 10
b. 11 
c. 14
d. 40
e. None of the above

The correct answer is: 11




Question 8

Consider the tree below. What is the order of nodes visited using a pre-order traversal?

       14
      /   \
     2    11
    / \     /  \
   1  3  10 30
            /  /           
          7   40


Select one:
a. 1 2 3 7 10 11 14 30 40
b. 1 2 3 14 7 10 11 40 30
c. 1 3 2 7 10 40 30 11 14
d. 14 2 1 3 11 10 7 30 40 

The correct answer is: 14 2 1 3 11 10 7 30 40




Question 9

Consider the tree in the previous question. What is the order of nodes visited using an in-order traversal?

Select one:

a. 1 2 3 7 10 11 14 30 40
b. 1 2 3 14 7 10 11 40 30 
c. 1 3 2 7 10 40 30 11 14
d. 14 2 1 3 11 10 7 30 40


The correct answer is: 1 2 3 14 7 10 11 40 30


No comments:

Post a Comment