ProgrammingModerateCognizant GenC · TCS NQT

What is printed?

What is printed?
class A:
    def __init__(self, v):
        self.v = v
    def __eq__(self, o):
        return isinstance(o, A) and self.v == o.v
xs = [A(1), A(2)]
print(A(2) in xs, A(3) in xs, xs.index(A(2)), A(1) != A(1))
  1. A. True False 1 True
  2. B. False False 1 False
  3. C. True False 0 False
  4. D. True False 1 False

Correct answer: D. True False 1 False

Python derives != from __eq__ when __ne__ is not defined, so A(1) != A(1) is False. The in and index tests use the same equality.

More Python Programming questions

Exams that ask this topic