ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class Key:
def __init__(self, v): self.v = v
def __hash__(self): return hash(self.v)
def __eq__(self, o): return self.v == o.v
k = Key(1)
d = {k: "one"}
k.v = 2
print(d.get(Key(1)), d.get(Key(2)), d.get(k), len(d))- A. one None None 1
- B. None two one 1
- C. one None one 1
- D. None None None 1
Correct answer: D. None None None 1
The dict stored k in the bucket for hash(1). After mutation, Key(1) hashes to that bucket but is not equal to k (now v = 2); Key(2) and k hash elsewhere and find nothing. The entry is effectively lost.
More Python Programming questions
- What is printed? python d = dict.fromkeys(range(3), 0) for k in d: d[k] += k e = {k: [] for k in "ab"} e["a"].append(1) f =…
- What is printed? python x = None print(x is None, x == None, bool(None), type(None).name, None is not False)
- What is printed? python class P: pass p = P() p.n = 1 p.n += 1 P.m = 10 q = P() q.m += 5 print(p.n, P.m, q.m, p.m, vars(q))
- What is printed? python def makeacc(): total = [] def add(x): total.append(x) return sum(total) def reset(): nonlocal total total = []…
- What is printed? python adders = [lambda x, n=n: x + n for n in range(3)] mults = list(map(lambda n: (lambda x: x n), range(3)))…
- What is printed? python def outer(): fs = [] for i in range(3): def f(j=None): return i if j is None else j fs.append(f) if i == 1: break…