A recursive function f(n) contains a local variable x initialized to 0. If the initial call is f(3) and it makes recursive calls f(2), f(1), and f(0) in a single linear chain, how many distinct, isolated copies of the local variable x are created in total across all frames?
C
Step-by-Step Solution
Key idea: Every recursive call creates a new, isolated stack frame with its own local variables.
Step 1: Identify the sequence of function calls: f(3), f(2), f(1), f(0).
Step 2: Count the total number of function invocations in this chain. There are 4 calls in total.
Step 3: Apply the mental model of stack frames. Each of the 4 calls creates a distinct stack frame.
Step 4: Since x is a local variable initialized inside the function, a new, isolated copy of x is created in each of the 4 frames.
Step 5: The total number of distinct copies is 4.
Answer: C