GateAI
Runtime Environments

GATE CSE 2023 · Q36

MCQ2 marksMedium

Time

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?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: A)

Runtime Environments

An activation tree represents the function calls during the execution of a program, where each node corresponds to an activation of a procedure, the root is the activation of the main function, and the children of a node represent the procedure calls made by that activation in the chronological order of their calls (from left to right).

  1. Root node (main):

    • main() sequentially calls:

      • First: f1()

      • Second: f2(2)

      • Third: f3()

    • Hence, main has three children from left to right: f1, f2, and f3.

  2. Activation of f2(2):

    • Inside f2(2) (with X=2X = 2):

      • It first calls f3().

      • Since X≠1X \neq 1, it enters the else branch and calls f2(1).

    • Thus, the node f2 (representing f2(2)) has two children: f3 and f2.

  3. Activation of f2(1):

    • Inside f2(1) (with X=1X = 1):

      • It first calls f3().

      • Since $X == 1$, it enters theif branch and calls f1().

    • Thus, this inner f2 node has two children: f3 and f1.

  4. Neither f1() nor f3() makes any further calls, so their nodes are leaf nodes.

This call hierarchy precisely matches the tree shown in Option (A).

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Runtime Environments previous year question is Q36 of the GATE CSE 2023 paper. Find more GATE CSE PYQs on Runtime Environments in Compiler Design, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Runtime Environments PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Runtime Environments in GATE CSE

Runtime Environments has 7 GATE CSE PYQs across 7 papers from 2012 to 2023. 3 of them are 2-mark MCQs like this one, and it is the only one in GATE CSE 2023.

How GATE tests Runtime Environments

More from GATE CSE 2023

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2023 · Q36 · MCQ

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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.