ProgrammingHardTCS CodeVita · Infosys HackWithInfy · Product Company SDE
What does the following Python 3 code print? from functools import lru_cache calls = 0 @lru_cache(None) def f(n): global calls calls += 1 return n if n < 2 else f(n - 1) + f(n - 2) print(f(14), calls)
- A. 233 14
- B. 377 15
- C. 15 377
- D. 15 14
Correct answer: B. 377 15
With memoisation each n from 0..N runs the body exactly once, so calls = N+1 while the Fibonacci value is unchanged. Output: 377 15.
More Python Programming questions
- What does the following Python 3 code print? a, b = set([12,12,1,7,1]), set([12,12,3,10,6,10]) print(sorted(a ^ b), len(a - b))
- What does the following Python 3 code print? def mk(): c = 0 def inc(): nonlocal c c += 2 return c return inc a, b = mk(), mk() for in…
- What does the following Python 3 code print? a, b, c = 25, 3, 17 print((a << b) ^ c, a & ~c, (a | c) >> b)
- What does the following Python 3 code print? def f(): try: return 7 finally: print("f", end=" ") x = f() + 13 print(x)
- What does the following Python 3 code print? a, b = [3,1,8,5,4,7], [7,4,2,5] print(list(map(lambda p: p[0] p[1], zip(a, b)))…
- What does the following Python 3 code print? d = {} for w in ["kheer","ghee","bun","nut","cake","jam"]: d[w[0]] = d.get(w[0], 0) + 1…