ITPEC FE Subject B October 2024 Question 8
From the answer group below, select the correct answer to be inserted into blank in the description.
This program performs operations on a priority queue.
A priority queue is a queue where the handled elements have a priority assigned to them, and the elements are extracted with the order of the highest priority first. The class PrioQueue represents a priority queue. The Figure shows an explanation of the class PrioQueue. Here, the priority is the integer value 1, 2, or 3, and the smaller the value the higher the priority.
When the procedure prioSched is called, the order of the output is blank.
Figure Explanation of the class PrioQueue
Answer group
Explanation
"J", "M", "F", "K", "E", "L" — the smaller number comes out first, and within one priority the element that was added earlier.
The while loop then empties the queue: priority 1 gives J, M (J was added first), priority 2 gives F, K, and priority 3 gives E, L.
Answer (c)
Why not others:
- •(a) — breaks ties newest first, like a stack inside each priority: the third dequeue then removes J instead of I, and the end order is M, I, then K, F, then L, E
- •(b) — ignores priorities and works as a plain stack: H, G and J are removed, and the rest come out newest first
- •(d) — serves the largest number first (priority 3 before 1), and still contains I, which the third dequeue removed
Key rule: Trace the dequeues in the middle of the program too, not only the final loop; a priority queue orders by priority first and by arrival only among equal priorities.
Stack · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.