Advanced Topics MCQ
Code Tracing: Heaps and Priority Queues
Practice mode shows one question at a time and lets students check each answer before moving on.
For practice use only.
Code Tracing: Heaps and Priority Queues
Code Tracing: Heaps and Priority Queues with immediate answer checks and explanations for Advanced Topics in Computer Science.
Question 1 of 25
Answered 0 of 25
Choose one answer.
Trace min-priority-queue polls and inserted follow-up values. What is printed?
import java.util.PriorityQueue;
public class MinHeapTrace
{
public static void main(String[] args)
{
int[] data = {9, 3, 7, 1, 5, 11};
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for (int value : data) pq.add(value);
String out = "";
for (int i = 0; i < 3; i++)
{
int first = pq.remove();
out += first + " ";
pq.add(first + 6);
}
String rest = "";
while (!pq.isEmpty()) rest += pq.remove() + " ";
System.out.println(out.trim() + ":" + rest.trim());
}
}