Skip to main content

Advanced Topics MCQ

Code Tracing: Heaps and Priority Queues

All AT CS Tests

Code Tracing: Heaps and Priority Queues

Question 1 of 25

Answered 0 of 25

Choose one answer.

What is printed after the minimum-priority-queue operations?

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());
    }
}
Choose one answer.