Starting with an empty stack, the operations push 1, push 2, push 3, pop, push 4, pop, pop, push 5, pop are performed. In what order are values popped?
Correct answer D. 3, 4, 2, 5
Explanation
Trace the stack: after pushing 1, 2, 3 the top is 3, so the first pop gives 3. Push 4 then pop gives 4. The next pop removes 2, leaving only 1. Push 5 and pop gives 5. So the output is 3, 4, 2, 5 and 1 remains on the stack. Each pop always takes the most recent item still present.
Answer checked How we write and check questions
Discussion