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


Correct Option: B

AI Explanation

To answer this question, let's go through each line of code and analyze the output:

Line 13: Creates a PriorityQueue object pq of type String.

Line 14: Adds the element "2" to the PriorityQueue pq.

Line 15: Adds the element "4" to the PriorityQueue pq.

Line 16: Prints the element at the front of the PriorityQueue using the peek() method. The element at the front is "2".

Line 17: Adds the element "1" to the PriorityQueue pq using the offer() method.

Line 18: Adds the element "3" to the PriorityQueue pq.

Line 19: Removes the element "1" from the PriorityQueue pq.

Line 20: Prints and removes the element at the front of the PriorityQueue using the poll() method. The element at the front is "2".

Line 21: Checks if the element "2" can be removed from the PriorityQueue using the remove() method. Since it is present, the condition is true. It then prints and removes the element at the front of the PriorityQueue using the poll() method. The element at the front is "2".

Line 22: Prints and removes the element at the front of the PriorityQueue using the poll() method. The element at the front is "3". It also prints the element at the front of the PriorityQueue using the peek() method. The element at the front is "4".

Therefore, the result is "2 2 3 4".

The correct answer is B.

Find more quizzes: