Skip to main contentSkip to solution

What will be the output of the following python code?

result=0

nstack=[40,70,80]

nstack.append(50)

result=result+nstack.pop()

nstack.append(30)

result=result+nstack.pop()

print("Result=", result)

Solution

✅ Correct Option: 1

Each pop removes the last item added, so the stack is traced line by line.

Statementnstackresult
nstack=[40,70,80][40, 70, 80]0
nstack.append(50)[40, 70, 80, 50]0
result=result+nstack.pop() (pops 50)[40, 70, 80]50
nstack.append(30)[40, 70, 80, 30]50
result=result+nstack.pop() (pops 30)[40, 70, 80]80

result=50+30=80result = 50 + 30 = \boxed{80}

The code prints Result= 80.

Keyboard Shortcuts

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