ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
class A:
x = 1
class B(A):
pass
class C(A):
x = 3
class D(B, C):
pass
B.x = 2
del B.x
print(D.x, D.__mro__.index(C))- A. 1 2
- B. 2 2
- C. 3 3
- D. 3 2
Correct answer: D. 3 2
D's MRO is D, B, C, A, object (C at index 2). After B.x is added and deleted again, B has no own x, so lookup reaches C.x = 3 before A.x.
More Python Programming questions
- What is printed? python class Shape: sides = 0 def describe(self): return f"{type(self).name}:{self.sides}" class Tri(Shape): sides = 3…
- What is printed? python class Money: def init(self, v): self.v = v def lt(self, o): return self.v < o.v def repr(self): return…
- What is printed? python class A: def eq(self, o): print("A.eq", end=" ") return NotImplemented class B(A): def eq(self, o): print("B.eq"…
- What is printed? python class Lazy: def init(self): self.calls = 0 @property def value(self): self.calls += 1 return 42 from functools…
- What is printed? python class Pos: def setname(self, owner, name): self.name = "" + name def get(self, obj, typ=None): return getattr(obj…
- What is printed? python def f(n): return [i for i in range(n) if i % 3 == 0 if i % 2 == 0] g = lambda n: [i for i in range(n) if i % 3 ==…