Menu
Topics Index
...
`

PriorityQueue


If you need explanation Read this topic

If you need Answer Take test on this topic

PriorityQueue
What will be the output of the following program?
import java.util.*;

   public class Book {
    public static void main(String[] args) {
        PriorityQueue<String> bk = new PriorityQueue<String>();
        bk.add("paper");
        bk.add("pen");
        bk.add("pencil");
        System.out.println(bk.poll() + "," + bk.peek());
    }
}

A. paper,pen
B. pencil,paper
C. paper,paper
D. pencil,pencil
E. Compilation Error or Runtime Error
Topic: Java PriorityQueue - PriorityQueue In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

C

Posted by Kommeta Narsing    2014-05-09 03:41:11


Here PriorityQueue is sorted based on the lexicographical order (dictionary order) of strings. So it looks like ---> paper, pen, pencil
poll() method retrieves and removes the head element (paper in this case) whereas peek() method only retrieves the head element (pen in this case as paper has already been removed.) So, the answer is A.

Posted by Shaileshwar Sharma    2014-05-09 04:15:20


Congratulations Shaileshwar Sharma. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-05-12 04:23:29


© meritcampus 2019

All Rights Reserved.

Open In App