Skip to main contentSkip to solution

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()

Solution

✅ Correct Option: 1

append(40) puts 40 on top, so the stack is [10,20,30,40][10, 20, 30, 40]. pop() removes and returns the top element (last in, first out).

The first pop returns 40, leaving [10,20,30][10, 20, 30]. The second pop returns 30, leaving [10,20][10, 20].

result=0+40+30=70result = 0 + 40 + 30 = \boxed{70}

Keyboard Shortcuts

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