Question
Consider the following Java code snippet:
import java.util.PriorityQueue; public class HeapQuestion5 { public static void main(String[] args) { PriorityQueue pq = new PriorityQueue(); pq.add("apple"); pq.add("banana"); pq.add("cherry"); System.out.println(pq.peek()); System.out.println(pq.size()); System.out.println(pq.peek()); } } What will be the output of this program?Solution
PriorityQueue for String uses natural ordering (alphabetical) by default, making it a min-heap. 1. pq.add("apple"); pq.add("banana"); pq.add("cherry"); The elements are added. "apple" is the smallest alphabetically. 2. System.out.println(pq.peek()); peek() retrieves the head of the queue (the highest-priority element) *without removing it*. So, "apple" is printed. 3. System.out.println(pq.size()); The size of the queue is still 3, as peek() does not remove elements. So, 3 is printed. 4. System.out.println(pq.peek()); Again, peek() retrieves the head without removal. "apple" is still the head. So, "apple" is printed. Therefore, the output is apple, 3, apple.
वचन की दृष्टि से कौन सा शब्द-युग्म सुमेलित नहीं है ?
निम्नलिखित प्रश्न में विषम शब्द का चयन करे ?
' किसी के पीछे-पीछे चलनेवाला ' वाक्यांश के लिए निम्नलिख�...
'हवन की सामग्री' के लिए एक शब्द क्या होगा?
निम्नलिखित प्रश्न में विषम शब्द का चयन करे ?
निम्नलिखित में से संधि नियमों के उल्लंघन की दृष्टि से अशु�...
Manuscript’ शब्द का अर्थ है –
‘ दीप्ति’ का पर्यायवाची है—
निम्नलिखित में से कौन सा वाक्य शुद्ध है?
नश्वर