mport java.util.*;class Queue1 { public static void main(String args[]) { PriorityQueue<String> queue = new PriorityQueue<String>(); queue.add("Amit"); queue.add("Vijay"); queue.add("Karan"); System.out.println("Printing queue elements:"); Iterator itr = queue.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } queue.remove(); System.out.println("Printing queue elements:"); Iterator<String> itr2 = queue.iterator(); while (itr2.hasNext()) { System.out.println(itr2.next()); } }}Select one:a.Printing queue elements:AmitVijayKaranPrinting queue elements:Karanb.Printing queue elements:AmitVijayKaranPrinting queue elements:KaranVijayc.Printing queue elements:AmitVijayKaranPrinting queue elements:AmitVijayd.Printing queue elements:AmitVijayKaranPrinting queue elements:Amit
Question
mport java.util.*;class Queue1 { public static void main(String args[]) { PriorityQueue<String> queue = new PriorityQueue<String>(); queue.add("Amit"); queue.add("Vijay"); queue.add("Karan"); System.out.println("Printing queue elements:"); Iterator itr = queue.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } queue.remove(); System.out.println("Printing queue elements:"); Iterator<String> itr2 = queue.iterator(); while (itr2.hasNext()) { System.out.println(itr2.next()); } }}Select one:a.Printing queue elements:AmitVijayKaranPrinting queue elements:Karanb.Printing queue elements:AmitVijayKaranPrinting queue elements:KaranVijayc.Printing queue elements:AmitVijayKaranPrinting queue elements:AmitVijayd.Printing queue elements:AmitVijayKaranPrinting queue elements:Amit
Solution
The correct answer is:
b. Printing queue elements: Amit Vijay Karan Printing queue elements: Karan Vijay
Explanation: The PriorityQueue class in Java does not order the elements in FIFO manner. It inherits AbstractQueue class. It holds the elements or objects which are to be processed by their priorities. Priority can be integer or string that is provided by the user. In this case, the PriorityQueue is using the natural ordering of the strings. So, when the elements are dequeued, they come out in their natural ordering, not in the order they were added. After the remove() operation, the element with the least natural ordering remains, which are "Karan" and "Vijay".
Similar Questions
import java.util.*;class Queue1 { public static void main(String args[]) { PriorityQueue<String> queue = new PriorityQueue<String>(); System.out.println(queue.element()); }}
What is the output of the following program:import java.util.*;class Queue1 { public static void main(String args[]) { PriorityQueue<String> queue = new PriorityQueue<String>(); queue.add("Amit"); queue.add("Vijay"); queue.add("Karan"); System.out.println("Printing queue elements:"); Iterator itr = queue.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } queue.remove(); System.out.println("Printing queue elements:"); Iterator<String> itr2 = queue.iterator(); while (itr2.hasNext()) { System.out.println(itr2.next()); } }}
What is the output of the following program:import java.util.*;class Queue1 { public static void main(String args[]) { PriorityQueue<String> queue = new PriorityQueue<String>(); System.out.println(queue.element()); }}Select one:a. None of the choicesb. Program will throw an exceptionc. No outputd. n
What is the output of the following code?import java.util.*;public class Main { public static void main(String[] args) { Queue queue = new LinkedList<>(); queue.add(1); queue.add(2); queue.add(3); System.out.println(queue.poll()); System.out.println(queue.poll()); System.out.println(queue.poll()); }}a)3 1 2b)3 2 1c)1 3 2d)1 2 3
What would be the output of the following code?import java.util.LinkedList;import java.util.Queue;class Test { public static void main (String[] args) { Queue<Integer> q=new LinkedList<Integer>(); for(int i=1;i<5;i++) { q.add(10+i); } System.out.print(q.poll()+" "+q.size()); }}
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.