ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
from functools import lru_cache
calls = 0
@lru_cache(maxsize=2)
def f(x):
global calls
calls += 1
return x
for v in [1, 2, 1, 3, 2, 1]:
f(v)
print(calls, f.cache_info().hits)- A. 3 3
- B. 4 2
- C. 6 0
- D. 5 1
Correct answer: D. 5 1
With room for 2 entries and LRU eviction: 1 miss, 2 miss, 1 hit, 3 miss (evicts 2), 2 miss (evicts 1), 1 miss. That is 5 misses (calls) and 1 hit.
More Python Programming questions
- What is printed? (Output lines are shown separated by " / ".) python from functools import partial def power(base, exp): return base exp…
- What is printed? python x = [3, 1, 2] y = sorted(x) z = reversed(x) x.append(0) print(y, list(z))
- What is printed? python m = map(lambda v: v 2, [1, 2, 3]) f = filter(None, [0, 1, 2]) print(list(m), list(m), next(f), list(zip(f, "xyz")))
- What is printed? python a = [1, 2, 3] for i, v in enumerate(a): a[i] = v 2 if i == 0: a.append(9) if len(a) > 4: break print(a)
- What is printed? python x = 0 def f(): global x x += 1 return x d = {f(): f(), f(): f()} L = [f(), f()] L[f() - 7] = f() print(d, L)
- What is printed? python def f(): return f.n f.n = 5 g = f f = lambda: 0 try: print(g()) except AttributeError: print("AE", g.n)