Question:

Consider the following program:
int main() {
    f1();
    f2(2);
    f3();
    return(0);
}

int f1() {
    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?

Show Hint

When drawing activation trees, always expand function calls step by step, including recursive calls. Each call becomes a node, and its direct calls become its children.
Updated On: Aug 26, 2025
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

Step 1: Understand function calls in main().
The function main() calls:
\[ f1(); f2(2); f3(); \]
So, the activation tree root (main) has three children: \(f1\), \(f2\), and \(f3\).

Step 2: Analyze f1().
f1() calls itself recursively once and then returns 1.
So \(f1\) has a recursive self-call in the activation tree.

Step 3: Analyze f2(2).
For \(f2(2)\):
- It first calls \(f3()\).
- Since \(X = 2 \neq 1\), it returns \(2 \times f2(1)\).
- Now \(f2(1)\) calls \(f3()\) and then returns \(f1()\).

So inside \(f2(2)\), we have calls: \(f3 \to f2(1) \to (f3, f1)\).

Step 4: Analyze f3().
f3() simply returns 5 with no further calls.

Step 5: Activation Tree Construction.
- Root: main
- Child 1: \(f1\) (recursive call inside)
- Child 2: \(f2(2)\)
- \(f3\)
- \(f2(1)\)
- \(f3\)
- \(f1\)
- Child 3: \(f3\)

This exactly matches option (A).

\[ \boxed{\text{Correct Option: (A)}} \]
Was this answer helpful?
0
0

Top Questions on Engineering Mathematics

View More Questions

Questions Asked in GATE CS exam

View More Questions