ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class A:
def __init__(self, **kw):
print("A", sorted(kw), end=" ")
class B(A):
def __init__(self, b=0, **kw):
print("B", end=" ")
super().__init__(**kw)
class C(A):
def __init__(self, c=0, **kw):
print("C", end=" ")
super().__init__(**kw)
class D(B, C):
pass
D(b=1, c=2, z=3)- A. B A ['c', 'z']
- B. B C A ['z']
- C. B C A ['b', 'c', 'z']
- D. C B A ['z']
Correct answer: B. B C A ['z']
Cooperative multiple inheritance: D's MRO is D, B, C, A. Each __init__ consumes its own keyword and passes the rest along via super(), so A finally receives only z.
More Python Programming questions
- What is printed? python class Base: def run(self): return self.step() def step(self): return "base" class Child(Base): def step(self)…
- What is printed? python class Meta(type): def new(m, name, bases, ns): ns["kind"] = name.upper() return super().new(m, name, bases, ns)…
- What is printed? python class S: slots = ("a",) class T(S): pass t = T() t.a = 1 t.b = 2 s = S() try: s.b = 2 except AttributeError…
- What is printed? python class Account: rate = 0.05 def init(self, bal): self.bal = bal def interest(self): return self.bal self.rate a =…
- What is printed? python def countergen(): c = 0 while True: cmd = yield c if cmd == "reset": c = 0 else: c += 1 g = countergen()…
- What is printed? python import itertools c = itertools.count() data = ["a", "b", "c"] pairs = list(zip(c, data)) print(pairs, next(c)…