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?
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 to node indicates that function calls function . 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():
main()first callsf1().main()then callsf2(2):- Inside
f2(2),f3()is called first. - Since , the
elsebranch is executed, callingf2(1).- Inside
f2(1),f3()is called first. - Since , the
ifcondition is met, callingf1().
- Inside
- Inside
- Finally,
main()callsf3().
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 f1This hierarchical structure corresponds to the tree shown in Option A.
Free practice · Your answers on this page are not saved.
