ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
d = {"k": []}
e = dict(d)
f = {k: v[:] for k, v in d.items()}
d["k"].append(1)
e["k"] += [2]
f["k"] += [3]
print(d["k"], f["k"])- A. [1] [3]
- B. [1, 2] [3]
- C. [1, 2, 3] [3]
- D. [1, 2] [1, 3]
Correct answer: B. [1, 2] [3]
dict(d) copies only the references, so e['k'] is d's list and += extends it in place, giving [1, 2]. f holds a separate slice copy made before the appends, so only 3 lands there.
More Python Programming questions
- 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])
- What is printed? python def f(x, acc={}): acc[x] = len(acc) return acc a = f("p") b = f("q", {}) c = f("r") print(a is c, a, b)
- What is printed? python def f(n, seen=[]): if n in seen: return seen seen = seen + [n] return seen print(f(1), f(2), f(1))
- What is printed? python import time def stamp(t=[]): t.append(len(t)) return t print(stamp(), stamp(), stamp.defaults)
- What is printed? python x = 5 def f(a, b=x 2, , c=x): return a + b + c x = 100 print(f(1), f(1, c=x))