ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
a = [1, 2] b = [a] * 3 a *= 2 b[0].append(9) print(b[2], len(b))
- A. [1, 2, 9] 3
- B. [1, 2] 3
- C. [1, 2, 1, 2] 3
- D. [1, 2, 1, 2, 9] 3
Correct answer: D. [1, 2, 1, 2, 9] 3
[a] * 3 stores three references to the same list object; a *= 2 extends that object in place and b[0].append(9) mutates it again, so every slot of b shows [1, 2, 1, 2, 9].
More Python Programming questions
- What is printed? python a = [1, 2] b = [a] 3 a = a 2 b[0].append(9) print(a, b[1])
- 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)