Exam-style question
Try this first
Explain why a program should test whether a stack is empty before using pop and test whether it is full before using push. Include the possible problem in each case and describe how peek differs from pop.
Answer and explanation
Concise answer
The program should test whether the stack is empty before pop so that it does not attempt to remove a value when no value is available.
Why this is correct
The tests prevent invalid operations at the boundaries of the stack. The distinction between peek and pop is whether the top element remains in the stack after its value is returned.
Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.
Common mistake
No common mistake is linked to this question yet.
