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)
  1. A. 3 3
  2. B. 4 2
  3. C. 6 0
  4. 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

Exams that ask this topic