ProgrammingModerateGATE CS · Product-company placement
What is printed? (Output lines are shown separated by " / ".)
What is printed? (Output lines are shown separated by " / ".)
class P:
def __init__(self, x): self.x = x
def __eq__(self, o): return self.x == o.x
a, b = P(1), P(1)
print(a == b, a != b, a is b)
try:
print({a})
except TypeError:
print("unhashable")- A. True False False / {P}
- B. False True False / unhashable
- C. True False False / unhashable
- D. True True False / unhashable
Correct answer: C. True False False / unhashable
Defining __eq__ without __hash__ sets __hash__ to None, so instances become unhashable. __ne__ is derived automatically by inverting __eq__.
More Python Programming questions
- What is printed? python class V: def init(self, n): self.n = n def add(self, o): return V(self.n + o) def radd(self, o): return V(o 10 +…
- What is printed? python class R: def init(self, n): self.n = n def len(self): return self.n def getitem(self, i): if i >= self.n: raise…
- What is printed? python class C: def init(self): self.d = {} def getattr(self, name): return f"<{name}>" def setattr(self, name, v)…
- What is printed? python class Temp: def init(self): self.c = 0 @property def c(self): return self.c @c.setter def c(self, v): if v < -273…
- What is printed? (Output lines are shown separated by " / ".) python class A: @staticmethod def s(): return "s" @classmethod def c(cls)…
- What is printed? python class Count: total = 0 def init(self): self.total += 1 Count.total += 10 a = Count(); b = Count() print(a.total…