ProgrammingVery hardTCS NQT · Wipro pattern

What is printed?

What is printed?
from functools import lru_cache
@lru_cache(maxsize=2)
def sq(x):
    return x * x
for v in [1, 2, 1, 3, 1]:
    sq(v)
print(sq.cache_info().hits, sq.cache_info().misses)
  1. A. 1 4
  2. B. 2 3
  3. C. 2 4
  4. D. 3 2

Correct answer: B. 2 3

Calls 1 and 2 miss, the second 1 hits, and 3 misses and evicts the least recently used key 2. The final 1 hits, giving 2 hits and 3 misses.

More Python Programming questions

Exams that ask this topic