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 :

Option 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 n1, 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).

Unlock Our Free Library

Access expert-curated educational resources and study materials—completely free.

Ask AI Tutor
5 left
Q1 View Question & Options
AI Tutor is solving this question...
Reading question context & options...