ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class K:
    def __eq__(self, other):
        return True

try:
    print({K(): 1})
except TypeError:
    print("TypeError")
  1. A. TypeError
  2. B. {<K object>: 1}
  3. C. RecursionError
  4. D. {}

Correct answer: A. TypeError

Defining __eq__ without __hash__ sets __hash__ to None, so instances become unhashable. Using one as a dict key raises TypeError: unhashable type.

More Python Programming questions

Exams that ask this topic