ITPEC FE Subject B April 2026 Question 8

Topic: Stack

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(51) and dequeue() are called in that order.

index0123456
item34undefinedundefined17132719

enqueue(51) is called.

index0123456
item3451undefined17132719

dequeue() is called.

index0123456
item3451undefinedundefined132719

Figure Queue with capacity of seven

[Program]
global: integer: capacity ← 7 /* Capacity of the queue */
global: integer []: item ← {7 undefined}
global: integer: count ← 0 /* Number of elements in the queue */
global: integer: front ← 0 /* Index of the front element */
global: integer: rear ← 0 /* Index of the element next to the rear */
○ boolean: enqueue(integer: val)
if (count A)
item[rear] ← val
count ← count + 1
rear ← (rear + 1) mod capacity
return true
else
return false
endif
○ integer: dequeue()
integer val
if (count > 0)
val ← item[front]
item[front] ← undefined
count ← count − 1
front ← B
return val
else
return undefined
endif

Answer group

OptionAB

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: enqueue may store a value only while the queue is not full, that is while count < capacity
  • •B: after the front element is removed, front moves one place forward and wraps past the end of the array, the same step rear takes in enqueue

Check against the figure, starting from front = 3, rear = 1, count = 5:

  • •enqueue(51) — 5 < 7, so item[1] becomes 51, count becomes 6 and rear becomes 2
  • •dequeue() — returns item[3], 17, clears that slot, sets count to 5 and front to 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 front backwards: 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.