ProgrammingModerateProduct-company MCQ pattern
What is printed?
What is printed?
a = [[0] * 3] * 3 a[0][0] = 1 print(a)
- A. [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
- B. [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
- C. [[1, 1, 1], [0, 0, 0], [0, 0, 0]]
- 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
- What is printed? python def f(x, lst=[]): lst.append(x) return lst f(1) f(2) print(f(3))
- What is printed? python x = 10 def f(): x += 1 return x try: f() except Exception as e: print(type(e).name)
- What is printed? python g = (i i for i in range(4)) print(sum(g), sum(g))
- What is printed? python print({1, 2, 3} ^ {2, 3, 4}, {1, 2} <= {1, 2, 3})
- What is printed? python a = "abc" b = a a += "d" print(a, b)
- What is printed? python t = (1, [2, 3]) t[1].append(4) print(t)