Multiple choice technology web technology

Given the proper import statement(s), and 13. PriorityQueue pq = new PriorityQueue(); 14. pq.add("2"); 15. pq.add("4"); 16. System.out.print(pq.peek() + " "); 17. pq.offer("1"); 18. pq.add("3"); 19. pq.remove("1"); 20. System.out.print(pq.poll() + " "); 21. if(pq.remove("2")) System.out.print(pq.poll() + " "); 22. System.out.println(pq.poll() + " " + pq.peek()); What is the result?

  1. 2 2 3 3

  2. 2 2 3 4

  3. 4 3 3 4

  4. 2 2 3 3 3

  5. 4 3 3 3 3

  6. 2 2 3 3 4

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

At line 16, pq.peek() returns '2'. Next, '1' is offered, '3' is added, and '1' is removed, leaving '2', '3', '4'. Line 20 polls and prints '2'. At line 21, pq.remove('2') returns false (as '2' was already polled), preventing the print inside the if-condition. Finally, line 22 polls '3' and peeks at '4'.