Question
Consider the following Java code snippet:
import java.util.PriorityQueue; public class HeapQuestion8 { public static void main(String[] args) { PriorityQueue pq = new PriorityQueue(); pq.add(5); pq.add(1); pq.add(5); // Adding a duplicate pq.add(3); System.out.println(pq.poll()); System.out.println(pq.poll()); System.out.println(pq.poll()); } } What will be the output of this program?Solution
A PriorityQueue (min-heap by default for Integers) correctly handles duplicate elements. They are treated like any other element based on their priority. 1. pq.add(5); pq.add(1); pq.add(5); pq.add(3); The elements in the heap are {1, 3, 5, 5} (conceptually, 1 is at the root). 2. System.out.println(pq.poll()); Removes and prints the smallest element: 1. The heap now contains {3, 5, 5}. 3. System.out.println(pq.poll()); Removes and prints the next smallest element: 3. The heap now contains {5, 5}. 4. System.out.println(pq.poll()); Removes and prints the next smallest element: 5. The heap now contains {5}. Therefore, the output is 1, 3, 5.
Which Constitutional Amendment Act provided for establishment of common high courts for two or more states?
The process of raising crops without using chemical fertilizers and pesticides or any other external material, Zero-budget natural farming (ZBNF), was i...
Under PM MUDRA Yojana, what is the maximum loan amount available under the "Tarun" category?
When was the National Interim Government of India formed, under Jawaharlal Nehru?
सूर्य का ताप पृथ्वी तक किस प्रक्रिया द्वारा पहुँचता है ?
IBSA is group of which of the following countries?
State Bank of India has inked a memorandum of understanding (MOU) with the ________ Cooperative Milk Producers Union to provide loan to individual dairy...
The idea/concept of Fundamental Rights in the Indian Constitution has been borrowed from:
'INS Vikramaditya' is a/an-
Which Indian bank first joined the Partnership for Carbon Accounting Financials (PCAF)?