ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
a = [1, 2] b = [a] * 3 a = a * 2 b[0].append(9) print(a, b[1])
- A. [1, 2, 1, 2] [1, 2, 9]
- B. [1, 2, 1, 2, 9] [1, 2, 1, 2, 9]
- C. [1, 2, 1, 2] [1, 2, 1, 2, 9]
- D. [1, 2, 9] [1, 2, 9]
Correct answer: A. [1, 2, 1, 2] [1, 2, 9]
a = a * 2 builds a NEW list and rebinds a, leaving b's three references pointing at the old [1, 2]; appending 9 through b[0] is therefore seen in b[1] but not in the new a.
More Python Programming questions
- What is printed? python t = ([1],) try: t[0] += [2] except TypeError: pass try: t[0] = t[0] + [3] except TypeError: pass print(t)
- What is printed? python x = [1, 2, 3] y = x x[:] = [7] x = x + [8] print(y, x)
- What is printed? python m = [[0] 2 for in range(2)] n = m.copy() n[0][0] = 5 n[1] = [6, 6] print(m)
- What is printed? python d = {"k": []} e = dict(d) f = {k: v[:] for k, v in d.items()} d["k"].append(1) e["k"] += [2] f["k"] += [3]…
- What is printed? python s = "abc" t = s s += "d" L = [1] M = L L += [2] print(t, M)
- What is printed? python row = [0] 3 grid = [row, row[:], row] grid[0][1] = 4 grid[1][2] = 7 print(grid[2], grid[1])