Stack · Class 12 · CBSE · Science Computer Science

What is stack underflow? State one condition that causes it and explain how it can be prevented in a Python implementation.

Practice question from Stack (Class 12 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

medium · Subjective · Q-1EB58E

What is stack underflow? State one condition that causes it and explain how it can be prevented in a Python implementation.

Answer: Stack underflow occurs when an attempt is made to remove or access an item from an empty stack. It can occur when pop() is performed while the stack contains no elements. In Python, it can be prevented by checking whether the list is empty before calling pop(), such as with if len(stack) == 0.

Solution

Underflow means attempting a deletion from an empty stack. Checking the stack's length before pop() prevents the operation from causing an error.

More from Stack

All of Stack

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed