What does the peek operation do in a stack?
Think about whether the element stays in the stack after the operation.
The peek operation returns the top element of the stack without removing it, allowing you to see what is on top without changing the stack.
Consider an empty stack. After pushing elements 5, 10, and 15 in that order, then popping once, what is the top element?
Remember that pop removes the top element.
After pushing 5, 10, 15, the stack top is 15. Popping removes 15, so the new top is 10.
What happens if you perform a pop operation on an empty stack?
Think about what it means to remove an element when none exist.
Trying to pop from an empty stack causes a stack underflow error because there is no element to remove.
Which statement correctly describes the difference between push and pop operations on a stack?
Think about how elements enter and leave the stack.
Push adds an element to the top of the stack, while pop removes the top element.
Starting with an empty stack, the following operations are performed in order: push(3), push(7), pop(), push(9), peek(). What is the value returned by the peek operation?
Track each operation step-by-step to see what remains on the stack.
After push(3) and push(7), stack top is 7. Pop removes 7, leaving 3. Push(9) adds 9 on top. Peek returns 9.