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())- A. A.eq B.eq False
- B. B.eq A.eq True
- C. B.eq A.eq False
- 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
- What is printed? python class Lazy: def init(self): self.calls = 0 @property def value(self): self.calls += 1 return 42 from functools…
- What is printed? python class Pos: def setname(self, owner, name): self.name = "" + name def get(self, obj, typ=None): return getattr(obj…
- What is printed? python def f(n): return [i for i in range(n) if i % 3 == 0 if i % 2 == 0] g = lambda n: [i for i in range(n) if i % 3 ==…
- What is printed? python a = {"x": 1, "y": 2} b = {v: k for k, v in a.items()} c = {k: v for k, v in zip("abc", [1, 2])} print(b, c, {1…
- What is printed? python a = b = [] a.append(1) c, d = [], [] c.append(1) x = y = 5 x += 1 print(b, d, y)
- What is printed? python a = [1, 2, 3] i = 0 i, a[i] = 2, 9 print(a, i)