ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
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):
print(n, end=" ")
return n * 2
f(3); f(3); f(4)
print(f(3))- A. 3 3 4 6
- B. 3 4 3 6
- C. 3 4 6
- D. 3 4 8
Correct answer: C. 3 4 6
The first f(3) computes and prints 3; the second is a cache hit; f(4) prints 4. The final f(3) is a cache hit too, returning 6 without printing.
More Python Programming questions
- 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=" ")…
- What is printed? python class A: x = "A" def get(self): return self.x class B(A): x = "B" b = B() A.x = "A2" b.x = "inst" del b.x…