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)
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.
| Statement | nstack | result |
|---|---|---|
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 |
The code prints Result= 80.
Related questions:
2025: 13 May Shift 2
2026: 13 May Shift 2
2026: 23 May Shift 1