Java OCA OCP Practice Question 2622

Question

Consider the following program:

import java.util.PriorityQueue;

public class Main {
        public static void main(String []args) {
               PriorityQueue<Integer> someValues = new PriorityQueue<Integer>();
                     someValues.add(new Integer(10));
                        someValues.add(new Integer(15));
                        someValues.add(new Integer(5));
                        Integer value;
                        while ((value = someValues.poll()) != null) {
                                System.out.print(value + " ");
                        }/*  w  ww  . j av a2 s.  c om*/
                }
        }

When executed, this program prints the following:

  • a) 10 10 10
  • b) 10 15 5
  • c) 5 10 15
  • d) 15 10 5
  • e) 5 5 5


c)

Note

The PriorityQueue prioritizes the elements in the queue according to its "natural ordering."

For integers, natural ordering is in ascending order, thus the output.

Note that the poll() method retrieves and removes the head of the queue if an element is available, or it returns null if the queue is empty.




PreviousNext

Related