A recursive function is given:
def mystery(n):
if n <= 0:
return 1
else:
return mystery(n-1) + mystery(n-2)
Find the value of mystery(4).
Correct Answer :
Solution :
The correct answer is 8.
To find the value of mystery(4), we can trace the recursive function step-by-step.
The definition of the function is:
For any input :
1. If , the function returns 1 (this is our base case).
2. Otherwise, for , the function returns:
Let us calculate the values starting from the smallest inputs up to 4:
Base Cases:
For any value less than or equal to 0, the function returns 1:
Step 1: Calculate mystery(1)
Step 2: Calculate mystery(2)
Step 3: Calculate mystery(3)
Step 4: Calculate mystery(4)
Thus, the value of mystery(4) is 8.
Access expert-curated educational resources and study materials—completely free.
Create, conduct, and manage professional online assessments with Mindyard. Perfect for teachers and institutes.
Copyright © 2026 Mindyard. All Rights Reserved.