Consider the following program:
\begin{verbatim}
void F(int n) {
if (n > 1) {
G();
F(n - 1);
F(n - 2);
}
}
void G() { }
\end{verbatim}
\textbf{Assertion (A):} If calls , the total number of activation records for function is 5.
\textbf{Reason (R):} The function is invoked exactly 5 times during this execution.
Which of the following is correct?
A
Step-by-Step Solution
Key idea: Activation records for a function = number of times that function is invoked.
Step 1: Trace execution for :
- : , so calls , ,
- : , so calls , ,
- from : , not , returns
- from : , not , returns
- from : , not , returns
Step 2: Count invocations:
- = 5 invocations
Step 3: Evaluate A and R:
- A: "activation records for is 5" = TRUE (5 invocations = 5 records)
- R: " is invoked exactly 5 times" = TRUE
- R explains A because activation records for = number of invocations.
Answer: Both A and R are true and R is the correct explanation of A.