ProgrammingModerateProduct-company MCQ pattern

What is printed?

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

Correct answer: A. [[1, 0, 0], [1, 0, 0], [1, 0, 0]]

[[0] * 3] * 3 copies the reference to ONE inner list three times. Changing a[0][0] changes that shared list, so every row shows the 1. Use [[0] * 3 for _ in range(3)] to get independent rows.

More Python Programming questions

Exams that ask this topic