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()); } }}
Question
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()); } }}
Solution
The output of the program will be:
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 (First-In-First-Out), but according to the natural ordering of its elements, or by a Comparator provided at queue construction time.
In this case, the PriorityQueue is using the natural ordering of its elements (which are Strings), and the natural ordering of Strings is lexicographic ordering.
So, when you add "Amit", "Vijay", and "Karan" to the queue, they are ordered as "Amit", "Karan", and "Vijay".
When you call queue.remove(), it removes the head of this queue (the smallest element with respect to the specified ordering), which is "Amit".
So, after removing the head, the queue contains "Karan" and "Vijay".
Similar Questions
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
import java.util.*;class Queue1 { public static void main(String args[]) { PriorityQueue<String> queue = new PriorityQueue<String>(); System.out.println(queue.element()); }}
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.