ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
from functools import lru_cache
import sys
sys.setrecursionlimit(1000)
@lru_cache(None)
def f(n):
return 0 if n == 0 else 1 + f(n - 1)
try:
print(f(1500))
except RecursionError as e:
print(type(e).__name__)- A. 1500
- B. 0
- C. MemoryError
- D. RecursionError
Correct answer: D. RecursionError
The cache only avoids repeated work. f(1500) first recurses 1500 levels deep before any value is cached, which exceeds the 1000 limit. Bottom-up loops avoid this completely.
More Python Programming questions
- What is printed? python g = {1: [2, 3], 2: [4], 3: [], 4: []} stack = [1] out = [] while stack: u = stack.pop() out.append(u)…
- What is printed? python s = list(range(10)) print(s[8:2:-3], s[::-4], s[-3:])
- What is printed? python a = [1, 2, 3, 4, 5] a[1:3] = [9] 4 print(a)
- What is printed? python print(repr('abc'[5:]), repr('abc'[-10:2]), [1, 2][3:0:-1])
- What is printed? python s = 'banana' print(s.find('an', 2), s.rfind('an'), s.find('x'), s.index('na', 3))
- What is printed? python print('aaaa'.count('aa'), 'aaaa'.replace('aa', 'b'), len('aaaaa'.split('aa')))