ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
class A:
    def __eq__(self, o):
        print("A.eq", end=" ")
        return NotImplemented
class B(A):
    def __eq__(self, o):
        print("B.eq", end=" ")
        return NotImplemented
print(A() == B())
  1. A. A.eq B.eq False
  2. B. B.eq A.eq True
  3. C. B.eq A.eq False
  4. D. A.eq False

Correct answer: C. B.eq A.eq False

If the right operand's type is a subclass of the left's and overrides the reflected method, it gets the first try: B.__eq__ runs first, then A.__eq__. Both return NotImplemented, so == falls back to identity comparison, which is False.

More Python Programming questions

Exams that ask this topic