Skip to main contentSkip to solution

Sumit wants to initially check the underflow condition in a stack before deleting an element from it. Which of the following are the correct options for it?

(A)

def isEmpty(stack):

if len(stack)==0:

print("underflow")

(B)

def isEmpty(stack):

if !(stack):

print("underflow")

(C)

def isEmpty(stack):

if stack==[] :

print("underflow")

(D)

def isEmpty(stack):

if len(stack)==None:

print("underflow")

Solution

✅ Correct Option: 4

Underflow in a stack means trying to delete from an empty stack, so the check must correctly detect an empty list.

(A) is correct because len(stack)==0 is true only when the list has no elements.

(C) is correct because stack==[] is true only when the list is empty.


(B): ! is not a Python operator for negation, so if !(stack): gives a syntax error. The correct form is if not stack:.

(D): len() always returns an integer, so it is never equal to None. The condition is always false and underflow is never reported.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question