Quiz Entry - updated: 2026.09.29
Starting from an empty stack, what do the calls push(5), push(3), pop(), push(7), push(9), top(), pop(), pop() return?
pop() → 3, top() → 9, pop() → 9, pop() → 7; the stack ends as (5).
* Each pop takes the most recent push that is still there. *
Trace it, bottom on the left, top on the right:
| Operation | Returns | Stack |
|---|---|---|
push(5) |
— | (5) |
push(3) |
— | (5, 3) |
pop() |
3 | (5) |
push(7) |
— | (5, 7) |
push(9) |
— | (5, 7, 9) |
top() |
9 | (5, 7, 9) |
pop() |
9 | (5, 7) |
pop() |
7 | (5) |
Two details are easy to get wrong. top() returns the top element but leaves it in place, so the next pop() returns the same 9. And the 5 pushed first is still at the bottom, because everything pushed after it has to be popped before it can come out. In this ADT, calling pop() on an empty stack returns null.