If the following operations are performed on a Queue, what will be the…

2016

If the following operations are performed on a Queue, what will be the sequence in which the elements are removed?

push(2), push(5), pop(), pop(), push(8), push(6), pop(), pop()

  1. A.

    6, 8, 5, 2

  2. B.

    5, 2, 6, 8

  3. C.

    5, 2, 8, 6

  4. D.

    2, 5, 8, 6

Attempted by 154 students.

Show answer & explanation

Correct answer: D

Concept

A Queue is a linear data structure governed by the FIFO (First In, First Out) principle: the element inserted earliest is the one removed earliest. Insertions add to the rear; removals always take from the front.

  • Naming note: the standard queue operations are enqueue() (insert at rear) and dequeue() (remove from front). Here push() and pop() are used as synonyms for those queue operations, so they still obey FIFO, not the LIFO behaviour those words imply for a stack.

Applying it step by step

Tracing each operation in order, where the front of the queue is on the left:

  1. push(2) → queue becomes [2].

  2. push(5)5 joins the rear → [2, 5] (front = 2).

  3. pop() → removes the front; 2 entered first, so 2 leaves first. Removed: 2. Queue: [5].

  4. pop() → removes the front; 5 leaves. Removed: 5. Queue: [].

  5. push(8)[8].

  6. push(6)6 joins the rear → [8, 6] (front = 8).

  7. pop()8 entered first, so it leaves first. Removed: 8. Queue: [6].

  8. pop()6 leaves. Removed: 6. Queue: [].

Cross-check and result

Reading the removed elements in the order they left gives 2, 5, 8, 6. This matches FIFO: within each push-push-pop-pop block the earlier-inserted value exits before the later one (2 before 5, then 8 before 6). A reversal within any pair would indicate LIFO (stack) behaviour, which a queue never shows.

Explore the full course: Btsc Lab Assistant