A function returns 0 for n = 0, 1 for n = 1, and fib(n-1) + fib(n-2) for larger n. What is fib(9)?
Answer and explanation
Correct answer: D. 34
- Step 1: The sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
- Step 2: fib(9) = 34.
Answer
34.
Why not the others: 21, 55 and 35 do not fit.
Practise more Python programming and algorithms.
Free questions with an explanation for every one, and full timed Saudi Secondary School Certificate mock tests.
More Python programming and algorithms questions
- What is the output of `print([x**2 for x in range(6) if x % 2 == 0])`?
- What is the output of `print([x**2 for x in range(4) if x % 2 == 0])`?
- What is the output of `print([x**2 for x in range(5) if x % 2 == 0])`?
- What is the output of `print([x**2 for x in range(4) if x % 3 == 0])`?
- What is the output of `print([x**2 for x in range(7) if x % 2 == 0])`?
- What does `'algorithm'[3:4]` return in Python?