Question Details

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).

Show Answer
Correct Answer
8

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 n:
1. If n≤0, the function returns 1 (this is our base case).
2. Otherwise, for n>0, the function returns:

mystery(n)=mystery(n-1)+mystery(n-2)

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:

mystery(0)=1

mystery(-1)=1

Step 1: Calculate mystery(1)

mystery(1)=mystery(0)+mystery(-1)

mystery(1)=1+1=2

Step 2: Calculate mystery(2)

mystery(2)=mystery(1)+mystery(0)

mystery(2)=2+1=3

Step 3: Calculate mystery(3)

mystery(3)=mystery(2)+mystery(1)

mystery(3)=3+2=5

Step 4: Calculate mystery(4)

mystery(4)=mystery(3)+mystery(2)

mystery(4)=5+3=8

Thus, the value of mystery(4) is 8.

Transparent Access on Mindyard

Built for Learners & Educators Alike

Completely free for students. Educators & coaching institutes get 2 months free Pro trial to create tests.

FREE FOR STUDENTS

Practice & Learn Free for Students

Access unlimited practice questions, detailed step-by-step solutions, and exam study materials — completely free for students.

  • Unlimited mock tests & question bank practice
  • Full access to curated study library & formula notes
  • 100% free for students • No credit card required
2 MONTHS FREE TRIAL

Educators & Coaching Institutes

Create and conduct professional online assessments with AI question generation, tab proctoring, and comprehensive batch management.

  • AI question uploader & rapid test generator
  • Anti-cheating tab proctoring & performance reports
  • Full Pro access free for 60 days • No upfront commitment
Discover more resources

You may also like

Mock Tests

View All
  • SSC
  • intermediate
  • 1 hour
  • english, general awareness, general intelligence and reasoning, logical reasoning, quant

SSC CHSL Tier-1 Shift 1 2 July 2024 question paper with answers and detailed solutions. This practice paper covers questions from General Intelligence & Reasoning, General Awareness, Quantitative Aptitude, and English Language as asked in the SSC CHSL Tier-1 examination. Practice the SSC CHSL 2 July 2024 Shift 1 paper to understand the exam pattern, question types, difficulty level, and important topics for upcoming SSC CHSL exams.

  • SSC
  • intermediate
  • 1 hour
  • english, general awareness, general intelligence and reasoning, logical reasoning, quant

SSC CHSL Tier-1 Shift 2 held on 2 July 2024 question paper with answers and detailed solutions. This previous year paper covers General Intelligence & Reasoning, Quantitative Aptitude, General Awareness, and English Language. The SSC CHSL Tier-1 exam was conducted in CBT mode with 100 questions for 200 marks and a 60-minute duration.

Ask AI Tutor
5 left
Q1 View Question & Options
AI Tutor is solving this question...
Reading question context & options...
Ask your own doubt or choose a quick prompt: