ProgrammingModerateGATE CS · Product-company placement

What is printed? (Output lines are shown separated by " / ".)

What is printed? (Output lines are shown separated by " / ".)
class P:
    def __init__(self, x): self.x = x
    def __eq__(self, o): return self.x == o.x
a, b = P(1), P(1)
print(a == b, a != b, a is b)
try:
    print({a})
except TypeError:
    print("unhashable")
  1. A. True False False / {P}
  2. B. False True False / unhashable
  3. C. True False False / unhashable
  4. D. True True False / unhashable

Correct answer: C. True False False / unhashable

Defining __eq__ without __hash__ sets __hash__ to None, so instances become unhashable. __ne__ is derived automatically by inverting __eq__.

More Python Programming questions

Exams that ask this topic