Stack · Class 12 · CBSE · Science Computer Science

What will be displayed by the following Python code? st = [] for n in [3, 6, 9]: st.append(n) while st: print(st.pop(), end=" ")

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

medium · MCQ · Q-03C01D

What will be displayed by the following Python code? st = [] for n in [3, 6, 9]: st.append(n) while st: print(st.pop(), end=" ")

  1. A.3 6 9
  2. B.9 6 3Correct answer
  3. C.3 9 6
  4. D.6 3 9

Solution

The values are appended in the order 3, 6, 9. Since pop() removes the most recently added element, they are printed in reverse order: 9 6 3.

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