ProgrammingHardTCS NQT · Wipro pattern
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:
s = {P(1)}
except TypeError:
print('unhashable')- A. True
- B. unhashable
- C. False
- D. no error, the set has one item
Correct answer: B. unhashable
Defining __eq__ without __hash__ sets __hash__ to None in the class, so instances cannot go into a set or be used as dict keys. The set literal raises TypeError.
More Python Programming questions
- What is printed? python class S: slots = ('a',) s = S() s.a = 1 try: s.b = 2 except AttributeError: print('no dict', s.a)
- What is printed? python print(len(set(map(lambda x: x % 3, range(10)))), sum(1 for in filter(None, [0, 1, '', 'a', None])))
- What is printed? python def mpow(b, e, m): r = 1 b %= m while e: if e & 1: r = r b % m b = b b % m e >>= 1 return r print(mpow(2, 10…
- What is printed? python import math n = 1016 - 1 print(math.isqrt(n), int(n 0.5))
- What is printed? python print(sorted([('b', 2), ('a', 2), ('c', 3)], key=lambda t: (-t[1], t[0])))
- What is printed? python def ok(s): def chk(l, r): while l < r: if s[l] != s[r]: return False l += 1 r -= 1 return True l, r = 0, len(s)…