Question:medium

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: Feb 3, 2026
Show Solution

The Correct Option is A

Solution and Explanation

To understand the activation tree of the given program, let's analyze the flow of the main function:

  1. The main() function calls f1().
  2. The f1() function is called, and within this function, it calls itself recursively, leading to an infinite recursion.
  3. After f1(), f2(2) is called.
  4. Inside f2(2):
    • It first calls f3(), which returns 5.
    • Then it checks if X == 1, which is not true for the first call as X = 2.
    • It returns 2 * f2(1).
    • Inside f2(1), f3() is called again, returning 5.
    • Since X == 1 is true, it calls f1(), causing infinite recursion.
  5. The last call is f3(), which simply returns 5.

Conclusion: The program contains multiple recursive calls to f1() leading to infinite recursion. The activation tree will show these recursive calls. Option (A) is the correct representation of the activation tree as it includes indirect recursion under f2(2).

Was this answer helpful?
0

Top Questions on Engineering Mathematics