ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
x = [1, 2, 3]
print(x.pop(), x.pop(0), x, x.insert(0, 9) or x)
  1. A. 3 1 [9, 2] [9, 2]
  2. B. 3 1 [2] [9, 2]
  3. C. 3 1 [2] [2]
  4. D. 3 1 [9, 2] [2]

Correct answer: A. 3 1 [9, 2] [9, 2]

Print evaluates all its arguments before printing. pop removes 3 and then 1, the list becomes [2], and then insert puts 9 in front. Both references to x show the final list.

More Python Programming questions

Exams that ask this topic