Skip to content
Official PYQ · 2023

GATE CS 2023 Q26 — Runtime Environments

Explain with AI

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?

Choose one option
View worked solution

Answer: A

An activation tree depicts the execution flow of a program, where each node represents an activation of a function, and an edge from node AA to node BB indicates that function AA calls function BB. Sibling nodes are ordered from left to right in the order of their invocation.

Let us trace the sequence of function calls starting from main():

  1. main() first calls f1().
  2. main() then calls f2(2):
    • Inside f2(2), f3() is called first.
    • Since X=2≠1X = 2 \neq 1, the else branch is executed, calling f2(1).
      • Inside f2(1), f3() is called first.
      • Since X=1X = 1, the if condition is met, calling f1().
  3. Finally, main() calls f3().

Note: The semicolon after if (X==1) in the code is a typographical error in the original GATE question and should be ignored for the logical flow.

The resulting hierarchical activation tree is:

      main
    /   |   \
   f1   f2   f3
       /  \
      f3  f2
         /  \
        f3  f1

This hierarchical structure corresponds to the tree shown in Option A.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Runtime Environments questions