ProgrammingVery hardProduct Company SDE · Python internals
What does the following Python 3 code print? def fib(n, memo={}): if n in memo: return memo[n] if n <= 1: return n memo[n] = fib(n - 1, memo) + fib(n - 2, memo) return memo[n] print(fib(10))
- A. 55
- B. 89
- C. 144
- D. 34
Correct answer: A. 55
Memoized Fibonacci: fib(10) follows the standard sequence 0,1,1,2,3,5,8,13,21,34,55, so fib(10)=55.
More Python Programming questions
- What does the following Python 3 code print? def outer(): x = 10 def inner(): x = 20 return x inner() return x print(outer())
- What does the following Python 3 code print? def outer(): x = [10] def inner(): x[0] = 20 inner() return x print(outer())
- What does the following Python 3 code print? x = 100 def f(): print(x) x = 5 try: f() except UnboundLocalError: print("UnboundLocalError")
- What does the following Python 3 code print? print("%d-%s-%.2f" % (5, "hi", 3.14159))
- What does the following Python 3 code print? print(list(filter(lambda x: x % 2 == 0, range(10))))
- What is printed? python print(type(10 / 5))