ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
calls = []
def trace(fn):
calls.append(fn.__name__)
return fn
class C:
@trace
def m(self): pass
@staticmethod
@trace
def s(): pass
@property
@trace
def p(self): return 1
print(calls, C().p, type(C.__dict__["s"]).__name__)- A. ['m', 's', 'p'] 1 function
- B. ['m', 'p'] 1 staticmethod
- C. ['m', 's', 'p'] 1 staticmethod
- D. ['m', 's', 'p'] <bound method> staticmethod
Correct answer: C. ['m', 's', 'p'] 1 staticmethod
trace runs once per decorated def, in class-body order, on the plain function. C.__dict__ keeps the staticmethod object itself, while property makes C().p return the value 1.
More Python Programming questions
- What is printed? python def memo(fn): cache = {} def w(n): if n not in cache: cache[n] = fn(n) return cache[n] return w @memo def f(n)…
- What is printed? python def register(cls): cls.tag = cls.name.lower() return cls @register class Alpha: tag = "none" class Beta(Alpha)…
- What is printed? python class A: def hi(self): return "A" class B(A): pass class C(A): def hi(self): return "C" class D(B, C): pass…
- What is printed? python class A: def f(self): return "A" class B(A): def f(self): return "B" + super().f() class C(A): def f(self): return…
- What is printed? python class X: pass class Y: pass class A(X, Y): pass class B(Y, X): pass try: class C(A, B): pass print("ok") except…
- What is printed? python class Base: def init(self): print("Base", end=" ") class L(Base): def init(self): print("L", end=" ")…