Question

Consider the following program:

int main()      int f1()          int f2(int X)                    int f3()
{               {                 {                                {
   f1();           return(1);        f3();                            return(5);
   f2(2);       }                   if (X==1)                      }
   f3();                               return f1();
   return(0);                       else
}                                      return (X*f2(X-1));
                                  }

Which one of the following options represents the activation tree corresponding to the main function?

Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2023 answer key

Solution

Trace the call hierarchy of function activations starting from main().

  1. main() sequentially calls:
    • f1()
    • f2(2)
    • f3()
  2. Inside f2(2):
    • It calls f3().
    • Since X=2≠1X = 2 \neq 1, it executes the else branch: return (X * f2(X-1)), invoking f2(1).
  3. Inside f2(1):
    • It calls f3().
    • Since X=1X = 1, it executes the if branch: return f1(), invoking f1().
  4. Therefore, the activation tree has main at the root with three child branches (f1, f2(2), f3). Under f2(2), there are children f3 and f2(1). Under f2(1), there are children f3 and f1. This corresponds exactly to Tree (A).
Final answer

Option (A)

Common trap

Overlooking that f2 calls f3 unconditionally at the very beginning of its body on each invocation.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2023 Computer Science and Information Technology (CS)
Question
36 of 65
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.