ProgrammingHardTCS CodeVita · Infosys HackWithInfy · Product Company SDE
What does the following Python 3 code print? a = [[0] * 2] * 2 a[0][0] = 7 print(a)
- A. [[7, 0], [0, 0]]
- B. [7, 0]
- C. [[7, 0], [7, 0]]
- D. [7, [0, 0]]
Correct answer: C. [[7, 0], [7, 0]]
[row] * m repeats references to the same inner list, so writing through one row shows in all rows. Output: [[7, 0], [7, 0]].
More Python Programming questions
- What does the following Python 3 code print? print(sum(x x for x in range(30) if x % 4 == 1))
- What does the following Python 3 code print? def f(x, acc=[]): acc.append(x) return acc f(8) f(4) print(f(8))
- What does the following Python 3 code print? fs = [lambda: i 2 for i in range(4)] print([f() for f in fs])
- What does the following Python 3 code print? a, b = set([2,10,5,12,11,10]), set([12,5,9,3]) print(sorted(a ^ b), len(a - b))
- What does the following Python 3 code print? def mk(): c = 0 def inc(): nonlocal c c += 4 return c return inc a, b = mk(), mk() for in…
- What does the following Python 3 code print? s = "committee" print(s.count("m"), s.find("m"), s.rfind("m"), s.index("m", s.find("m") + 1)…