Consider the following operations performed on a stack of size 5 : Push (a);…

2016

Consider the following operations performed on a stack of size 5 :

Push (a); Pop() ; Push(b); Push(c); Pop();

Push(d); Pop();Pop(); Push (e)

Which of the following statements is correct ?

  1. A.

    Underflow occurs

  2. B.

    Stack operations are performed smoothly

  3. C.

    Overflow occurs

  4. D.

    None of the above

Attempted by 1080 students.

Show answer & explanation

Correct answer: B

Simulate the operations (top shown on the right):

  1. Push(a) => [a]

  2. Pop() => []

  3. Push(b) => [b]

  4. Push(c) => [b, c]

  5. Pop() => [b]

  6. Push(d) => [b, d]

  7. Pop() => [b]

  8. Pop() => []

  9. Push(e) => [e]

Key checks:

  • No underflow: every Pop() removed an element that existed at that time; no Pop() was called on an empty stack.

  • No overflow: the stack capacity is 5 and the maximum number of elements present at once was 2, so no Push() exceeded capacity.

Conclusion:

All operations are performed smoothly. The final stack contains e (with e at the top).

Explore the full course: Coding For Placement