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.
Which elite Naval Air squadron in the Andaman & Nicobar Command carried out the maiden all-women maritime surveillance mission on March 08, 2024? Â
Who established the Chishti sect in India?
Which bank has become the first to officially introduce the Mahila Samman Savings Certificate (MSSC) scheme?Â
In case of sugarcane, the planting material is obtained from:
Which of the following sports was included as a discipline in the 11 th Asian Games Beijing 1990?Â
__________ introduced the virtual currency as a legal tender and became the first ever country to do so.
The dress of Rajasthani 'Tara Bhant ki Odhni' is popular in -
Where is the headquarters of ESAF Small Finance Bank located?
The following major rivers of Uttarakhand state will be in decreasing order according to their length:
1. Kosi
2. Bhagirathi
3. Kali
4. Alaknanda
With which of the following Financial Institution, Gruh Finance was merged?Â