ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class A:
def __init__(self): self.v = 1
def __eq__(self, o): return isinstance(o, A) and self.v == o.v
__hash__ = None
class B(A):
def __hash__(self): return 7
s = {B(), B()}
print(len(s), B() in s, A() == B())- A. 2 False True
- B. 1 True True
- C. 1 False True
- D. TypeError
Correct answer: B. 1 True True
B restores hashing (all hash to 7) and inherits A's __eq__, so the two B() objects are equal and collapse into one set item; another B() is found. A() == B() is True since B is an A with the same v.
More Python Programming questions
- What is printed? python print(isinstance(True, int), issubclass(bool, int), type(True) is int, True == 1, hash(True) == hash(1.0))
- What is printed? python s = {1, 2, 3} s.add(2) s.discard(9) s.update([3, 4], (5,)) r = s.pop() if False else len(s) try: s.remove(9)…
- What is printed? python d = {"a": 1, "b": 2} items = d.items() print(("a", 1) in items, ("a", 2) in items, items & {("b", 2), ("c", 3)} ==…
- What is printed? python d = dict(a=1) d2 = d d = {d, "b": 2} d2["c"] = 3 del d2["a"] print(d, d2, dict([("x", 1), ("x", 2)])…
- What is printed? python def f(a, b=[]): b = b or [] b.append(a) return b x = f(1) y = f(2, []) z = f(3, [0]) print(x, y, z, f.defaults)
- What is printed? python def f(x=[]): x += [len(x)] return x def g(x=()): x += (len(x),) return x print(f(), f(), g(), g())