Skip to main contentSkip to solution

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 35+25=6035 + 25 = \boxed{60}.


Option 2: this comes from treating pop() as removing from the front, so 5+15=205 + 15 = 20. In a stack, pop() removes the most recently added element.

Option 4: this comes from forgetting the append(35), so the pops give 25+15=4025 + 15 = 40.

Keyboard Shortcuts

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