Menu
Topics Index
...
`

Remove3003


If you need explanation Read this topic

If you need Answer Take test on this topic

Remove3003
What will be the output of the following program?
import java.util.*;
public class Sample {
    public static void main(String s[])
    {
        PriorityQueue<String> bk = new PriorityQueue<String>();
        bk.add("paper");
        bk.add("pen");
        bk.add("pencil");
        System.out.println(bk);
    }
}

A. [pencil]
B. []
C. Some other output
D. 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 ic C..here we have the PriorityQueue Concept..in PQ elements are sorted according to the their natural sorting order based on the constructor..and PQ dont allow the null values to insert..and PQ will compare the values so non-comparable objects wont allow..

Execution:
here we create the instance for the PQ which stores the String objects..by using the add() we are adding the "paper" and "pen" and "pencil" .next we are printing the Q using Sop so it will print the elements in PQ so it will print ["paper" and "pen" and "pencil"]

Posted by Uday Kumar    2015-01-28 03:41:35


Ans is Option C.... Execution starts from main() method here we are creating object for PriorityQueue collection with String values after that we are inserting                          (or adding) String values to that object using bk.add("paper") statements so in bk object we have the values paper,pen,pencil.... the elements of priority queue is ordered and it is first in first out so it will print [paper,pen,pencil] using println statement so Option C is Ans....

Posted by Bhagi Bhagyasri    2015-01-28 04:20:12


I'll go with Option C ..here we have used Collection called PriorityQueue .. and used add method thrice .. so .. all the 3 elements added will be there in QUEUE and when we print it using sysout 3 elements added will be displayed in square braces separated by "COMMAS" ...

Posted by ?????????? ?????    2015-01-28 05:31:42


ANS IS C

OUTPUT: [ paper, pen ,pencil ]

--Here,the concept of Priority Queue is used.

--We know that Queue follows First-In-First-Out
model but sometimes we need to process the objects
in the queue based on the priority. PriorityQueue is an unbounded Queue implementation in Java, which is based on priority heap.
PriorityQueue allows you to keep elements in a particular order,
according to there natural order or custom order defined by Comparator interface in Java. Head of priority queue data structure
will always contain least element with respect to specified ordering.

--PriorityQueue class was introduced in "Java
1.5" and part of Java Collections Framework.

--In above program,object of priorityQueue class
is created by importing "util" package.

--Object of priorityQueue can hold String elements.
add() method is used to add the elements in the
priorityQueue.

--Object is printed using system.out.println().
output will be paper,pen,pencil.the elements
are displayed in the order they were added.

Posted by Mânïshå Mùlchåndânï    2015-01-28 14:51:49


c

Posted by Shubham Bansal    2015-01-28 19:59:39


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Bhagi Bhagyasri,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Gaurav Bansal. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-29 06:11:07


© meritcampus 2019

All Rights Reserved.

Open In App