ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
class A:
    def __init__(self): self.v = 1
    def __eq__(self, o): return isinstance(o, A) and self.v == o.v
    __hash__ = None
class B(A):
    def __hash__(self): return 7
s = {B(), B()}
print(len(s), B() in s, A() == B())
  1. A. 2 False True
  2. B. 1 True True
  3. C. 1 False True
  4. D. TypeError

Correct answer: B. 1 True True

B restores hashing (all hash to 7) and inherits A's __eq__, so the two B() objects are equal and collapse into one set item; another B() is found. A() == B() is True since B is an A with the same v.

More Python Programming questions

Exams that ask this topic