ProgrammingVery hardGATE CS · Product-company placement
What is printed? (Output lines are shown separated by " / ".)
What is printed? (Output lines are shown separated by " / ".)
class A:
@staticmethod
def s(): return "s"
@classmethod
def c(cls): return cls.__name__
def i(self): return type(self).__name__
class B(A): pass
print(B.c(), B().c(), B().i(), A.s() + B().s())
try:
A.i()
except TypeError:
print("TE")- A. B B B ss / TE
- B. A A B ss / TE
- C. B B A ss / TE
- D. B B B ss / A
Correct answer: A. B B B ss / TE
A classmethod receives the class it is called on (B, whether through the class or an instance). Calling the plain method A.i() without an instance is missing self and raises TypeError.
More Python Programming questions
- What is printed? python class Count: total = 0 def init(self): self.total += 1 Count.total += 10 a = Count(); b = Count() print(a.total…
- What is printed? python class Base: def initsubclass(cls, prefix="x", kw): super().initsubclass(kw) cls.tag = prefix + cls.name class…
- 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"…