ProgrammingModerateProduct-company MCQ pattern
What is printed?
What is printed?
calls = 0
def f(n):
global calls
calls += 1
return 1 if n <= 1 else f(n - 1) + f(n - 2)
print(f(5), calls)- A. 8 8
- B. 8 15
- C. 8 9
- D. 5 15
Correct answer: B. 8 15
f(0) = f(1) = 1, so f(5) = 8. The call count follows C(n) = 1 + C(n-1) + C(n-2) with C(0) = C(1) = 1: C(2) = 3, C(3) = 5, C(4) = 9, C(5) = 15.
More Python Programming questions
- What is printed? python import copy a = [[1, 2], [3]] b = copy.copy(a) c = copy.deepcopy(a) a[0].append(9) a.append([4]) print(len(b)…
- What is printed? python x = [1] 3 for i in range(3): x[i] = x[i - 1] + i print(x)
- What is printed? python x = "g" def outer(): x = "o" def inner(): global x x = "i" inner() return x print(outer(), x)
- What is printed? python s = " Py Thon " print(s.strip().lower().replace(" ", "").count("o"), s.split(" ").count(""))
- What is printed? python def f(x): try: r = 10 // x except ZeroDivisionError: return "Z" else: return r finally: print("F", end=" ")…
- What is printed? python class P: def repr(self): return "R" def str(self): return "S" print(P(), [P()], f"{P()!r}")