ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def repeat(n):
print("R", n, end=" ")
def deco(fn):
def w(x):
return [fn(x) for _ in range(n)]
return w
return deco
@repeat(2)
def sq(x):
return x * x
print(sq(3))- A. [9, 9]
- B. R 2 [9, 9]
- C. R 2 9
- D. R 2 [9, 9, 9]
Correct answer: B. R 2 [9, 9]
repeat(2) is called first (printing R 2) and its result deco is applied to sq. Calling sq(3) runs the wrapper, which calls the original twice.
More Python Programming questions
- What is printed? python calls = [] def trace(fn): calls.append(fn.name) return fn class C: @trace def m(self): pass @staticmethod @trace…
- 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…