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))
  1. A. one None None 1
  2. B. None two one 1
  3. C. one None one 1
  4. 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

Exams that ask this topic