ProgrammingHardGATE CS · Product-company placement

What is printed?

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

Correct answer: C. [0, 4, 0] [0, 0, 7]

grid[0] and grid[2] are the same object (row), while grid[1] is an independent copy taken before any change; so 4 appears in rows 0 and 2 and 7 only in row 1.

More Python Programming questions

Exams that ask this topic