ProgrammingVery hardTCS NQT · Wipro pattern

What is printed?

What is printed?
from functools import lru_cache
@lru_cache(maxsize=None)
def get(n):
    return [n] * 2
a = get(3)
a.append(99)
b = get(3)
print(a, b, a is b)
  1. A. [3, 3, 99] [3, 3, 99] True
  2. B. [3, 3, 99] [3, 3] False
  3. C. [3, 3] [3, 3] False
  4. D. [3, 3, 99] [3, 3] True

Correct answer: A. [3, 3, 99] [3, 3, 99] True

lru_cache returns the same cached list object each time, so mutating a changes what later calls return. Cached results are shared, not copied.

More Python Programming questions

Exams that ask this topic