Question 1
A stack initially contains [5, 12, 8] where 8 is at the top. Trace the following operations and state the final state of the stack:
Pop(); Push(3); Push(7); Pop(); Peek(). [4]
1
Pop() removes 8 → stack is [5, 12], returns 8. Top is now 12.
1
Push(3) → stack is [5, 12, 3]. Push(7) → stack is [5, 12, 3, 7]. Top is now 7.
1
Pop() removes 7 → stack is [5, 12, 3], returns 7.
1
Peek() returns 3 (the current top element) WITHOUT removing it. Final stack: [5, 12, 3] with 3 at top.