Find the output of the following code using Stack:
result=0
numberList=[5,15,25]
numberList.append(35)
result=result+numberList.pop()
result=result+numberList.pop()
print(“Result=”,result)
Find the output of the following code using Stack:
result=0
numberList=[5,15,25]
numberList.append(35)
result=result+numberList.pop()
result=result+numberList.pop()
print(“Result=”,result)
Solution
✅ Correct Option: 1
append(35) adds 35 at the top of the stack, so the list becomes [5, 15, 25, 35]. pop() removes the last (top) element each time.
The first pop() gives 35, so result = 35. The second pop() gives 25, so result becomes .
Option 2: this comes from treating pop() as removing from the front, so . In a stack, pop() removes the most recently added element.
Option 4: this comes from forgetting the append(35), so the pops give .
Related questions:
2026: 18 May Shift 2
2026: 24 May Shift 1
2025: 16 May Shift 1