A stack is empty at the start. The following operations are done in order: push 4, push 9, pop, push 6, push 2, pop. Which element is now on top of the stack?
- (A)
2
- (B)
4
- (C)
6
- (D)
9
Answer and solution
Answer: (C)
After push 4 and push 9, the stack is 4, 9 with 9 on top.
Pop removes 9, so only 4 is left.
Push 6 and push 2 give 4, 6, 2 with 2 on top.
The last pop removes 2, so the top is 6.