Machine-translated by Google. Questions are authored in English.
Top

Discussion

What will be the output of the program?

import java.util.*;

public class Sketch33
{
    public static void main(String[] args)
    {
        Queue<Integer> pq = new PriorityQueue<Integer>(
            Arrays.asList(30, 10, 20));
        System.out.print(pq.poll());
        System.out.print(pq.poll());
        System.out.print(pq.poll());
    }
}

  • A.The program prints 102030
  • B.The program prints 203010
  • C.The order is unspecified.
  • D.The program prints 302010

Answer: A

A PriorityQueue hands elements out in priority order, which for Integer is ascending, so successive polls give 10, 20 then 30 regardless of insertion order. Note that only removal is ordered: printing the queue itself would show internal heap order, which is not sorted.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.