What will be the sequence of numbers, if the following operations are…

2016

What will be the sequence of numbers, if the following operations are performed on a Queue? push(9), pop(), push(5), push(7), pop(), pop(), push(8), pop()

  1. A.

    5, 7, 8, 9

  2. B.

    9, 5, 8, 7

  3. C.

    8, 7, 5, 9

  4. D.

    9, 5, 7, 8

Attempted by 96 students.

Show answer & explanation

Correct answer: D

A Queue works on the FIFO (First-In, First-Out) principle. This means the element that enters the queue first will be the first one to be removed.

  • push(x) / enqueue(x) adds an element to the rear (back) of the queue.

  • pop() / dequeue() removes and returns the element from the front of the queue.

Let us trace the operations step-by-step to find the sequence of elements that are popped (removed):

Step

Operation

Current State of Queue (Front to Rear)

Popped Element

1

push(9)

[9]

None

2

pop()

[]

9

3

push(5)

[5]

None

4

push(7)

[5, 7]

None

5

pop()

[7]

5

6

pop()

[]

7

7

push(8)

[8]

None

8

pop()

[]

8

Sequence of popped numbers: 9, 5, 7, 8

Therefore, Option D is the correct answer.

Explore the full course: Btsc Lab Assistant