Question

A custom stack implementation has a pop() method that is supposed to remove and return the top element. However, when the stack is empty, calling pop() causes an error (e.g., IndexError if using a list, or NullPointerException if using a linked list without proper checks). What is the most fundamental debugging step to prevent this underflow error?

A Ensure the push() method correctly adds elements.
B Implement a peek() method to view the top element without removing it.
C Add a check if not self.is_empty(): before attempting to access or remove an element.
D Increase the initial capacity of the stack.
E Use a try-except block around the pop() operation.
Practice Next

Hey! Ask a query

🎓
Think You're Ready for RBI Grade B?
RBI Grade B 2026 Phase 1 Memory Based Paper
  • 200 Questions with Detailed Solutions
  • Section-wise Coverage (GA, English, Quant & Reasoning)