ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
x = [0] * 3 y = [[0] * 2] * 2 y[0][1] = 7 print(x, y)
- A. [0, 0, 0] [[0, 0], [0, 7]]
- B. [0, 7, 0] [[0, 7], [0, 7]]
- C. [0, 0, 0] [[0, 7], [0, 7]]
- D. [0, 0, 0] [[0, 7], [0, 0]]
Correct answer: C. [0, 0, 0] [[0, 7], [0, 7]]
[[0] * 2] * 2 repeats one inner list object, so both rows are the same object and changing one changes the other. Use [[0] * 2 for _ in range(2)] for independent rows.
More Python Programming questions
- What is printed? python import bisect vals = [10, 20, 30] print(bisect.bisectleft(vals, 0), bisect.bisectleft(vals, 35)…
- What is printed? python from collections import deque d = deque('abcde') d.rotate(2) print(''.join(d), ''.join(deque('abcde').reversed()))
- What is printed? python print(int('0' 5 + '7') + int(' -3 '), len(str(int('9' 50) + 1)))
- What is printed? python import math print(math.isqrt(99), math.isqrt(100), round(2.5), round(3.5))
- What is printed? python import itertools print(list(itertools.accumulate([3, 1, 4, 1, 5], max))[-1], list(itertools.accumulate([3, 1, 4]…
- What is printed? python import heapq print(heapq.heappop([]) if False else 'ok', heapq.nlargest(1, []))