UGC-NET Dec 2012 · Stacks

MCQ · 2 marks · Problem PROG-DSTR-0036

Given an empty stack, after performing push(1), push(2), pop, push(3), push(4), pop, pop, push(5), pop, what is the value of the top of the stack?
  1. A
    4
  2. B
    3
  3. C
    2
  4. D
    1

Solve it on GATE DA practice →More Basic Data Structures problems

Show the worked solutionTrace the stack (bottom to top):\[\text{push 1: }[1],\ \text{push 2: }[1,2],\ \text{pop: }[1],\ \text{push 3: }[1,3],\ \text{push 4: }[1,3,4],\]\[\text{pop: }[1,3],\ \text{pop: }[1],\ \text{push 5: }[1,5],\ \text{pop: }[1].\]The top of the stack is 1.Answer: D

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com