ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class Base:
def __init_subclass__(cls, prefix="x", **kw):
super().__init_subclass__(**kw)
cls.tag = prefix + cls.__name__
class A(Base): pass
class B(Base, prefix="p_"): pass
class C(B): pass
print(A.tag, B.tag, C.tag, hasattr(Base, "tag"))- A. xA p_B p_C False
- B. xA p_B p_B False
- C. xA p_B xC True
- D. xA p_B xC False
Correct answer: D. xA p_B xC False
__init_subclass__ runs for every subclass when it is created, including indirect ones; C gets the default prefix 'x' because class keywords are not inherited. Base itself is never passed to its own hook.
More Python Programming questions
- What is printed? python class It: def init(self): self.i = 0 def iter(self): return self def next(self): self.i += 1 if self.i > 3: raise…
- What is printed? python class Ctx: def init(self, n): self.n = n def enter(self): print("in", self.n, end=" ") return self.n 10 def…
- What is printed? python from contextlib import contextmanager @contextmanager def cm(): print("A", end=" ") try: yield "B" except…
- What is printed? python from itertools import accumulate, chain, islice, count print(list(accumulate([1, 2, 3, 4])), list(chain("ab"…
- What is printed? python from itertools import ziplongest, product, combinations, permutations print(list(ziplongest("ab", [1, 2, 3]…
- What is printed? (Output lines are shown separated by " / ".) python from itertools import groupby data = [1, 1, 2, 1, 3, 3] print([(k…