Question:

In the below stack operation, what will be the output value for the variable 'result'?
result = 0
stack = [10, 20, 30]
stack.append(40)
result += stack.pop()
result += stack.pop()

Show Hint

In Python lists used as stacks:
- append() adds to the end (Top).
- pop() removes from the end (Top) in LIFO order.
Updated On: Sep 7, 2026
  • 70
  • 60
  • 90
  • 50
Show Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

Concept:
In Python, standard lists can be used as stacks.
The append(x) method acts as push, adding an element to the end (top) of the list.
The pop() method without arguments removes and returns the last item (top) of the list.

Step 1: Step-by-Step Execution of the Code:

- Initially, result = 0.
- stack = [10, 20, 30].
- stack.append(40) pushes 40 onto the top of the stack.
The stack is now: [10, 20, 30, 40].

Step 2: Evaluating the Pop Operations:

- First pop: stack.pop() removes and returns the topmost element, 40.
result += 40 updates result to $0 + 40 = 40$.
The stack is now: [10, 20, 30].
- Second pop: stack.pop() removes and returns the next topmost element, 30.
result += 30 updates result to $40 + 30 = 70$.
The stack is now: [10, 20].
Final Answer:
The final calculated value of result is 70, which corresponds to option (A).
Was this answer helpful?
0
0