ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
a = [[0] * 2 for _ in range(2)] b = [[0] * 2] * 2 a[0][1] = 5 b[0][1] = 5 print(a, b)
- A. [[0, 0], [0, 0]] [[0, 5], [0, 5]]
- B. [[0, 5], [0, 0]] [[0, 5], [0, 5]]
- C. [[0, 5], [0, 0]] [[0, 5], [0, 0]]
- D. [[0, 5], [0, 5]] [[0, 5], [0, 5]]
Correct answer: B. [[0, 5], [0, 0]] [[0, 5], [0, 5]]
Multiplying a list of lists copies only the outer reference, so both rows of b are the same list. Building each row with a comprehension gives independent rows.
More Python Programming questions
- What is printed? python print(divmod(7, 2), divmod(-7, 2), divmod(7.5, 2))
- What is printed? python def outer(): res = [] for i in range(3): res.append(lambda: i) return [f() for f in res] def outer2(): return…
- What is printed? python print(10 18 % 7, (10 18 + 2) // 4, -(2 63) // (2 62))
- What is printed? python lst = list(range(5)) lst[1:4] = [] try: lst[::2] = [9, 9] except ValueError: print('ValueError', lst)
- What is printed? python def dedupe(a): if not a: return 0 k = 1 for i in range(1, len(a)): if a[i] != a[k - 1]: a[k] = a[i] k += 1 return…
- What is printed? python def firsttrue(lo, hi, ok): while lo < hi: mid = (lo + hi) // 2 if ok(mid): hi = mid else: lo = mid + 1 return lo…