ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
a = [1, 2]
b = [a] * 3
a *= 2
b[0].append(9)
print(b[2], len(b))
  1. A. [1, 2, 9] 3
  2. B. [1, 2] 3
  3. C. [1, 2, 1, 2] 3
  4. D. [1, 2, 1, 2, 9] 3

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

[a] * 3 stores three references to the same list object; a *= 2 extends that object in place and b[0].append(9) mutates it again, so every slot of b shows [1, 2, 1, 2, 9].

More Python Programming questions

Exams that ask this topic