ProgrammingHardProduct-company MCQ pattern
What is printed on the first line?
What is printed on the first line?
grid = [[0] * 2] * 2 grid[0][0] = 1 print(grid) g2 = [[0] * 2 for _ in range(2)] g2[0][0] = 1 print(g2)
- A. [[1, 0], [0, 0]]
- B. [[1, 1], [0, 0]]
- C. [[1, 0], [1, 0]]
- D. [[0, 0], [1, 0]]
Correct answer: C. [[1, 0], [1, 0]]
[[0] * 2] * 2 repeats one inner list reference, so changing grid[0][0] changes both rows. A list comprehension builds a fresh inner list on each pass, which is why the second grid changes only row 0.
More Python Programming questions
- What is printed? python t = (1, [2]) t[1].append(3) print(t, len(t)) try: t[0] = 5 except TypeError as e: print(type(e).name)
- What is printed? python print(bin(0b1010 << 2 >> 1 & 0xF), 0o17 + 0x1F + 0b11, 0xff, 10j)
- What is printed? python x = 0 x |= 1 << 3 x &= ~(1 << 0) x ^= 0b111 print(x, x.bitcount(), (x >> 1) & -x)
- What is printed? python print(-(-231) // 1, (231 - 1) & -(231), 231 >> 31, 232 & -232)
- What is printed? python print((1 << 3) - 1 << 2, 1 << 3 - 1 << 2, 1 + 2 << 1, 3 & 2 2)
- What is printed? python print(0 0, 0.0 0, (-8) 0, (-2) 3, -2 3)