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")
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
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.
Related questions:
2025: 16 May Shift 1
2025: 14 May Shift 1