Java Queue Quiz

Home

Question: 0 1 2 3 4 5

Question Zero

a. What best describes the queue paradigm?


b. Placing an item in a queue is called?


c. Removing an item in a queue is called?


d. An algorithm, which traverses through graphs or trees, utilizes queues for its implementation. What is this algorithm called?


e. You can instantiate a type Queue to make a Queue object.






Question One

  public class QueueTest{
    public static void main(String[] args){
      Queue<Integer> orders = new LinkedList<Integer>();
      
      orders.add("Orange Juice");
      ordres.add("Mangoes");
      orders.add("Almond Milk");
      orders.add("Eggs");
      orders.add("Blueberries");

      String a = orders.poll();
      String b = orders.peek();
      String c = orders.poll();
      String d = orders.peek();
      System.out.println(a + ", " + b + ", " + c + ", " + d);
    }
  }
  

What will this print?

output =


Question Two

  public class QueueTest{
    public static void main(String[] args){
      Queue<Integer> queue = new LinkedList<Integer>();
      Stack<Integer> stack = new Stack<Integer>();
      
      for (int i = 1; i <= 9; i += 2)
         queue.add(i);
      
      while (!queue.isEmpty()) {
         stack.add(queue.poll());
      }
      while (!stack.isEmpty()) {
         queue.add(stack.pop());
      }

    }
  }
  

Which values will be contained in the queue after the two while loops? (from first to last, without square braces)

Example answer format (spacing between the numbers): 1, 2, 3, 4, 5

queue =


Question Three

  public class Test{
    public static void main(String[] args){
      PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
  
      /*
      a is first iteration, b is second iteration, ... , 
      e is fifth iteration
      */
      for (int i = 5; i >= 0; i--) {
         queue.add(i);
         System.out.println(queue); // 1. a, b, c, d, e, f
      }
      
      String result = "";
      while (!queue.isEmpty()) {
          result += queue.poll() + " ";
      }
      System.out.println(result.trim()); // 2. a.
  }
}
  

What are the print outs? (without the square braces)

1.

a = 5

b = 4, 5

c =


d =


e =


f =


2.

a =





































Question Four

  public class QueueTest{
    public static void main(String[] args){
      Queue<Integer> q1 = new LinkedList<Integer>();
      Queue<Integer> q2 = new LinkedList<Integer>();
      int limit = 5;

      for(int i = 0; i < limit; i++) {
         q1.add((int) Math.pow(i, i));
         q2.add((limit - i)*(limit - i));
      }
      System.out.println(q1); // 1.
      System.out.println(q2); // 2.
    }
  }
  

What are the print outs? (without the square braces)

1.


2.




Question Five

  public class Test{
    public static void main(String[] args){
      Queue<Character> q = new LinkedList<Character>();
      String s = "I love CMSC132 more than pizza!";

      for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
          q.add(s.charAt(i));
        }
      }

      System.out.println(q.size()); //1
    }
  }
  

What's the print out?

1.


back top Home