Menu
Topics Index
...
`

Priority Queue


If you need explanation Read this topic

If you need Answer Take test on this topic

Priority Queue
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.peek() + "," + bk.poll());
    }
}

A. paper,pencil
B. pencil,paper
C. paper,paper
D. pencil,pencil
E. pen,pen
F. 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.

Ans is C. peek method gives paper which is the head in the queue and is not removed and when poll method is called, it also returns the head i.e., paper and then removes paper from queue.

Posted by Nikhil Sulibhavi    2014-04-08 16:07:41


ans is C,peek method returns the head of the queue,and poll method removes head of the and returns the removed item.

Posted by Raviteja Daggupati    2014-04-08 17:52:51


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

Posted by Merit Campus    2014-04-09 03:56:53


© meritcampus 2019

All Rights Reserved.

Open In App