🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Matrix Traversal: all 13 questions

Quiz: Matrix Traversal · Question 5 of 13

What does this print? (code)

What does this print?

grid = [[0] * 3] * 2
grid[0][0] = 9
print(grid)
  1. A.[[9, 0, 0], [0, 0, 0]]
  2. B.[[9, 0, 0], [9, 0, 0]]
  3. C.[[9, 9, 9], [0, 0, 0]]
  4. D.Error
Answer & explanation

Correct answer: [[9, 0, 0], [9, 0, 0]]

Both rows are the same list object, so changing one changes "both". Build rows with a comprehension.

Can you get the rest right without peeking?

Take the timed Quiz: Matrix Traversal quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →