GATE 2023 CS – Question 36
Consider the following program:
int main()
{
f1();
f2(2);
f3();
return(0);
}
int f1()
{
return(1);
}
int f2(int X)
{
f3();
if (X==1)
return f1();
else
return (X*f2(X-1));
}
int f3()
{
return(5);
}Which one of the following options represents the activation tree corresponding to the main function?

Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (A) main → (f1, f2, f3); f2 → (f3, f2); inner f2 → (f3, f1)
Explanation
An activation tree has one node per procedure call, with children being the calls made by it, in order.
- `main` calls `f1()`, then `f2(2)`, then `f3()` (three children, in that order).
- `f2(2)` first calls `f3()`; since $X=2\ne1$ it then calls `f2(1)`.
- `f2(1)` first calls `f3()`; since $X=1$ it then calls `f1()`.
So the tree is: main $\to$ (f1, f2, f3); f2 $\to$ (f3, f2); the inner f2 $\to$ (f3, f1). This is option (A).