Stack · Class 12 · CBSE · Science Computer Science

Consider the following Python code: s = [] def push(x): s.append(x) def popitem(): if len(s) == 0: return "Underflow" return s.pop() push(10) push(20…

Practice question from Stack (Class 12 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

hard · MCQ · Q-3FD64D

Consider the following Python code: s = [] def push(x): s.append(x) def pop_item(): if len(s) == 0: return "Underflow" return s.pop() push(10) push(20) push(30) a = pop_item() push(40) b = pop_item() print(a, b, s[-1]) What will be printed?

  1. A.10 20 30
  2. B.30 40 20Correct answer
  3. C.30 20 40
  4. D.40 30 20

Solution

The first pop removes 30, so a becomes 30. After 40 is pushed, the next pop removes 40, so b becomes 40; the remaining stack is [10, 20], whose top is 20.

More from Stack

All of Stack

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed