The GATE Grind

GATE 2023 CS – Question 36

Compiler Design · Runtime Environments · 2 marks · Multiple choice

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?

four activation trees (A)-(D).
  1. main → (f1, f2, f3); f2 → (f3, f2); inner f2 → (f3, f1)
  2. main → (f2, f3); with f1 and f3 below
  3. main → f2; f2 → f1; with f3 f1 below
  4. main → (f1, f2, f3); f2 → (f3, f2, f1)

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

main calls f1, f2(2), f3 in order. f2(2) calls f3 and then f2(1). f2(1) calls f3 and then f1. This matches option A.