ITPEC IP April 2022 Question 5

In a queue, three (3) elements 33, 27, and 12 are enqueued in this order. After the element 45 is enqueued to this queue, two (2) elements are retrieved (or dequeued). Which of the following is the second element to be retrieved?

Explanation

27 — a queue is first-in, first-out, so the two elements retrieved are the two oldest, 33 then 27.

Elements join at the rear and leave from the front:

  • •After the four enqueues the queue, front to rear, is 33, 27, 12, 45
  • •The first dequeue removes 33
  • •The second dequeue removes 27

Answer (b)

Why not others:

  • •(a) 12 — would be the second element taken from a stack (last-in, first-out), after 45; in the queue it comes out third
  • •(c) 33 — is the first element retrieved, not the second
  • •(d) 45 — was enqueued last, so it is retrieved last; it would come out first only from a stack

Key rule: A queue (FIFO) removes elements in the order they were added; a stack (LIFO) removes the most recently added one first.

Algorithm and Programming · 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 IP past-paper collection or Report an issue.