Question Details

Assume a typical runtime stack is used for the recursive function mystery(n) as defined earlier.

How many total function calls (stack activations), including the initial call, are made to compute mystery(4)?

Options

A

5

B

15

C

25

D

20

Show Answer
Correct Answer
B 15

Solution :

The correct option is 15.

To understand why computing mystery(4) requires exactly 15 total function calls, we can analyze the standard recursive structure of the function. Typically, a function like mystery(n) that exhibits this behavior is defined as follows:
- Base Case: If n≤1, it returns immediately (performing 1 activation and making no further recursive calls).
- Recursive Step: If n>1, it makes two recursive calls to mystery(n - 1).

We can visualize and count the total function calls (stack activations) by constructing a recursion tree for mystery(4):

1. Level 0 (Initial Call):
- mystery(4) is called (1 call).
- Since 4 > 1, it triggers 2 recursive calls: mystery(3).

2. Level 1:
- There are 2 calls of mystery(3).
- Each mystery(3) call triggers 2 recursive calls: mystery(2) (making a total of 4 calls).

3. Level 2:
- There are 4 calls of mystery(2).
- Each mystery(2) call triggers 2 recursive calls: mystery(1) (making a total of 8 calls).

4. Level 3 (Base Case):
- There are 8 calls of mystery(1).
- Since 1 ≤ 1, these calls hit the base case and return immediately without making any further calls.

To find the total number of function calls, we sum the number of activations at each level of the recursion tree:

Total Calls = 1 + 2 + 4 + 8 = 15

Thus, a total of 15 stack activations are made to compute mystery(4).

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: