ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
class P:
def __init__(self, x):
self.x = x
def __eq__(self, o):
return self.x == o.x
try:
print(len({P(1)}))
except TypeError as e:
print(type(e).__name__)- A. AttributeError
- B. TypeError
- C. 1
- D. 0
Correct answer: B. TypeError
Defining __eq__ without __hash__ sets __hash__ to None, so instances are unhashable. Putting one in a set raises TypeError, which the except block prints.
More Python Programming questions
- What is printed? python from functools import reduce print(reduce(lambda a, b: a b, range(1, 21), 1), reduce(lambda a, b: a + b, [], 0))
- What is printed? python try: print(sorted([3, None, 1])) except TypeError as e: print(e)
- What is printed? python def flat(x): for y in x: if isinstance(y, list): yield from flat(y) else: yield y print(list(flat([1, [2, [3…
- What is printed? python print(len(str(int('7' 4300)))) try: int('7' 4301) except ValueError: print('no')
- What is printed? python print(round(0.25, 1), round(0.35, 1), round(2.5), round(-0.5))
- What is printed? python d = {'a': 1, 'b': 2} d['a'] = 3 d.pop('a') d['a'] = 4 print(list(d.items()))