Question
Consider the following Java code snippet: Â Â
import java.util.Collections; Â Â import java.util.PriorityQueue; Â Â public class HeapQuestion2 { Â Â Â Â public static void main(String[] args) { Â Â Â Â Â Â PriorityQueue maxHeap = new PriorityQueue(Collections.reverseOrder()); Â Â Â Â Â Â maxHeap.add(10); Â Â Â Â Â Â maxHeap.add(5); Â Â Â Â Â Â maxHeap.add(20); Â Â Â Â Â Â maxHeap.add(3); Â Â Â Â Â Â System.out.println(maxHeap.poll()); Â Â Â Â Â Â System.out.println(maxHeap.poll()); Â Â Â Â } Â Â } Â Â What will be the output of this program?Solution
By passing Collections.reverseOrder() to the PriorityQueue constructor, we create a max-heap, where the largest element has the highest priority (is at the root).   1. maxHeap.add(10); maxHeap.add(5); maxHeap.add(20); maxHeap.add(3);     After these additions, the heap contains {20, 10, 5, 3} (conceptually, 20 is at the root).   2. System.out.println(maxHeap.poll());     poll() removes and returns the largest element. So, 20 is removed and printed. The heap now contains {10, 5, 3}.   3. System.out.println(maxHeap.poll());     poll() removes and returns the largest element again. So, 10 is removed and printed. The heap now contains {5, 3}.   Therefore, the output is 20, 10.
What is the total cost of the Porunai Archaeological Museum inaugurated in Tamil Nadu?
According to the International Labour Organization (ILO) report, what is the projected global unemployment rate for the year 2024?
Which of the following bank adjudged as best performing bank in Self Help Group (SHG) Linkage by Deendayal Antyodaya Yojana – National Rural Livel...
What significant step did NTPC take to support the GREEN initiative?
Air India is establishing South Asia’s largest flight training school. Where will this training school be located?
Which of the following statements about Nokia and IISc partnership on 6G tech is/are correct?
1.Nokia has partnered with IISc to research 6G tech...
At what age did Anish Sarkar become the youngest rated chess player in history?
Haryana Chief Minister Manohar Lal Khattar announced a monthly pension of _________ for the Padma awardees from the state.
What is the initial investment for India’s first AI-focused Datacentre Park inaugurated in Raipur, Chhattisgarh?
The PM-AASHA scheme has been implemented to support remunerative prices for farmers. Which of the following is NOT a component of the scheme's strategy ...