ITPEC FE Subject B April 2026 Question 8
From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 0.
The program implements a queue that has a fixed capacity and only accepts integers. If the queue is not full, the function enqueue appends the specified value to its end and returns true. Otherwise, it returns false. If the queue is not empty, the function dequeue removes an element from the queue and returns its value. Otherwise, it returns undefined. The figure illustrates the working of a queue with capacity to hold up to seven integers. Initially, the queue contains five integers, with front, rear, and count set to 3, 1, and 5, respectively. Then, enqueue( and dequeue( are called in that order.
enqueue( is called.
dequeue( is called.
Figure Queue with capacity of seven
Answer group
Explanation
A: < capacity, B: (front + 1) mod capacity — a circular queue accepts a value while it is not full, and front advances and wraps the same way as rear.
- •A:
enqueuemay store a value only while the queue is not full, that is whilecount<capacity - •B: after the front element is removed,
frontmoves one place forward and wraps past the end of the array, the same stepreartakes inenqueue
Check against the figure, starting from front = 3, rear = 1, count = 5:
- •
enqueue(— 5 < 7, so51) item[1]becomes 51,countbecomes 6 andrearbecomes 2 - •
dequeue(— returns) item[3], 17, clears that slot, setscountto 5 andfrontto 4 mod 7 = 4, where 13 is the next oldest value
Answer (e)
Why not others:
- •(a) and (b) — test
count < 0, which is never true, so nothing can ever be enqueued - •(g) and (h) — test
count > capacity, which is also never true - •(c) and (d) — test
count > 0, which refuses the first value on an empty queue and overwrites the front element of a full one - •(b), (d), (f) and (h) — step
frontbackwards: in the figure it would move from 3 to 2, an empty slot, instead of to 13 at index 4
Key rule: In a circular buffer both indexes advance the same way, add 1 then take the remainder by the capacity; full and empty are told apart by count, because front equals rear in both states.
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.