chapter

    Runtime Environments and Procedure Calls Notes for Gate Cs

    Runtime Environments and Procedure Calls notes for GATE CS: 20 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questio

    Runtime Environments and Procedure Calls notes

    Activation Trees vs Call Graphs

    Activation Trees vs Call Graphs
    An activation tree represents the dynamic execution of a program, whereas a call graph represents the static structure of the code. A call graph has exactly one node for each unique procedure defined in the source code. An activation tree has one node for every single time a procedure is invoked during a specific run. If a recursive function calls itself five times, the call graph shows one node with a self-referential edge, but the activation tree shows five distinct nodes arranged in a parent-child hierarchy.
    Explain this more simply
    Think of a call graph as a blueprint of a factory. It shows which rooms exist and which doors connect them. An activation tree is the log of a specific day's production. If a worker walks through the same door ten times, the blueprint does not change, but the daily log records ten separate entries.
    Go one level deeper
    The activation tree is a strict tree with no cycles and a single root because execution is strictly nested. A call graph is a directed graph that may contain cycles and multiple entry points. The depth of the activation tree at any point equals the current call stack depth.

    Rules for Constructing Activation Trees

    Execution Trace

    To construct an activation tree from source code, follow a strict top-down execution trace. Begin with the main procedure as the root node. When a procedure P calls procedure Q, draw a directed edge from the current active node of P to a new child node representing this specific invocation of Q. The new node becomes the current active node. When Q returns, the active node reverts to the caller P. Repeat this for every function call in the execution order.

    Core Rules

    • One node per invocation, not per function name.
    • Edges represent the caller-callee relationship.
    • Active node shifts down on call, up on return.
    Explain this more simply
    Imagine you are drawing a family tree, but instead of generations, you are tracking phone calls. If Alice calls Bob, Bob is a child of Alice. If Bob then calls Charlie, Charlie is a child of Bob. When Bob hangs up, you go back to Alice. You never draw two Bobs as the same person if he makes two separate calls. Each call is a new node.
    Go one level deeper
    The standard method breaks if you attempt to draw the tree based on source code layout rather than execution order. The tree must reflect the dynamic sequence of calls, meaning conditional branches that evaluate to false produce no child nodes. The tree is a record of actual events, not potential ones.

    Building the Tree for the Anchor Example

    Consider a program where main calls A(2). Function A(int n) calls B(), and then conditionally calls A(n-1) if n > 1. Function B() calls C(), and C() simply returns. Construct the activation tree for this execution.
    The immediate instinct is to draw main, then A, then B, then C, and then another A, all in a single flat list or merging the two A calls into one node because they share the same name.
    Merging the two A calls violates the definition of an activation tree, which requires a distinct node per invocation. Furthermore, placing them flat ignores the nesting. A(n-1) is called by A(2), so it must be a child of the A(2) node, not a sibling.
    1. Start with root node: main.
    2. main calls A(2). Add child node A(2) under main. Active node is A(2).
    3. A(2) calls B(). Add child node B() under A(2). Active node is B().
    4. B() calls C(). Add child node C() under B(). Active node is C().
    5. C() returns. Active node reverts to B().
    6. B() returns. Active node reverts to A(2).
    7. A(2) checks n > 1 (2 > 1 is true). It calls A(1). Add child node A(1) under A(2). Active node is A(1).
    8. A(1) calls B(). Add child node B() under A(1).
    9. This second B() calls C(). Add child node C() under the second B().
    10. C() returns, B() returns, A(1) checks n > 1 (1 > 1 is false), A(1) returns, A(2) returns, main returns.
    Verification: The tree has main at the root. A(2) is the child of main. A(2) has two children: the first B() and A(1). The first B() has child C(). A(1) has child B(), which in turn has child C().

    17 more cards in this chapter

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    Level 1: Warm-up

    Consider the following program:

    \begin{verbatim}

    void F(int n) {

    if (n > 1) {

    G();

    F(n - 1);

    F(n - 2);

    }

    }

    void G() { }

    \end{verbatim}

    \textbf{Assertion (A):} If calls , the total number of activation records for function is 5.

    \textbf{Reason (R):} The function is invoked exactly 5 times during this execution.

    Which of the following is correct?

    Question 2
    Level 1: Warm-up

    Consider the following program:

    \begin{verbatim}

    void F(int n) {

    G();

    if (n > 0) {

    F(n - 1);

    }

    }

    void G() { }

    \end{verbatim}

    If calls , consider the following statements:

    \begin{itemize}

    \item : The activation tree contains exactly 3 nodes labeled .

    \item : The function is invoked exactly 2 times.

    \item : The maximum depth of the activation tree is 4.

    \end{itemize}

    Which of the following is correct?

    Question 3
    Level 1: Warm-up

    Consider the following program:

    \begin{verbatim}

    void main() {

    P();

    }

    void P() {

    Q();

    R();

    }

    void Q() { }

    void R() { }

    \end{verbatim}

    Which of the following statements is TRUE about the execution of this program?

    Question 4
    Level 1: Warm-up

    Consider the following program:

    \begin{verbatim}

    void main() {

    A();

    }

    void A() {

    B();

    C();

    }

    void B() {

    D();

    E();

    }

    void C() { }

    void D() {

    F();

    }

    void E() { }

    void F() { }

    \end{verbatim}

    When procedure F is executing, how many activation records are simultaneously present on the call stack?

    Question 5
    Level 1: Warm-up

    A program defines three functions: , , and . During execution, calls exactly twice. Each invocation of calls exactly three times. No other function calls occur. How many nodes does the activation tree for this execution contain?

    Question 6
    Level 1: Warm-up

    A program defines exactly four functions: , , , and . During a specific execution, calls three times, calls twice, and calls four times. No other calls occur. What is the number of nodes in the call graph for this program?

    Question 7
    Level 1: Warm-up

    Consider the following program:

    \begin{verbatim}

    void main() {

    P();

    Q();

    }

    void P() {

    R();

    R();

    R();

    }

    void Q() {

    S();

    }

    void R() {}

    void S() {}

    \end{verbatim}

    How many directed edges are present in the activation tree for this execution?

    Question 8
    Level 1: Warm-up

    Consider the following program:

    \begin{verbatim}

    void main() {

    X();

    Y();

    }

    void X() {

    Z();

    }

    void Y() { }

    void Z() { }

    \end{verbatim}

    What is the sequence of procedure returns during the execution of this program?

    Question 9
    Level 1: Warm-up

    Consider the following program:

    \begin{verbatim}

    void main() {

    A();

    }

    void A() {

    B();

    C();

    }

    void B() { }

    void C() { }

    \end{verbatim}

    In the sequence of procedure returns during the execution of this program, what is the position (1-indexed) of procedure C?

    Question 10
    Level 1: Warm-up

    Consider the following program:

    \begin{verbatim}

    void main() {

    P();

    Q();

    }

    void P() { R(); }

    void Q() { S(); }

    void R() { }

    void S() { }

    \end{verbatim}

    Which of the following statements are TRUE regarding the execution of this program?

    Free preview ends here

    Login to view the complete notes

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Runtime Environments and Procedure Calls Notes for GATE CS

    Runtime Environments and Procedure Calls notes for GATE CS: 20 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    Activation Trees vs Call Graphs

    Activation Trees vs Call Graphs
    An activation tree represents the dynamic execution of a program, whereas a call graph represents the static structure of the code. A call graph has exactly one node for each unique procedure defined in the source code. An activation tree has one node for every single time a procedure is invoked during a specific run. If a recursive function calls itself five times, the call graph shows one node with a self-referential edge, but the activation tree shows five distinct nodes arranged in a parent-child hierarchy.
    Explain this more simply
    Think of a call graph as a blueprint of a factory. It shows which rooms exist and which doors connect them. An activation tree is the log of a specific day's production. If a worker walks through the same door ten times, the blueprint does not change, but the daily log records ten separate entries.
    Go one level deeper
    The activation tree is a strict tree with no cycles and a single root because execution is strictly nested. A call graph is a directed graph that may contain cycles and multiple entry points. The depth of the activation tree at any point equals the current call stack depth.

    Rules for Constructing Activation Trees

    Execution Trace

    To construct an activation tree from source code, follow a strict top-down execution trace. Begin with the main procedure as the root node. When a procedure P calls procedure Q, draw a directed edge from the current active node of P to a new child node representing this specific invocation of Q. The new node becomes the current active node. When Q returns, the active node reverts to the caller P. Repeat this for every function call in the execution order.

    Core Rules

    • One node per invocation, not per function name.
    • Edges represent the caller-callee relationship.
    • Active node shifts down on call, up on return.
    Explain this more simply
    Imagine you are drawing a family tree, but instead of generations, you are tracking phone calls. If Alice calls Bob, Bob is a child of Alice. If Bob then calls Charlie, Charlie is a child of Bob. When Bob hangs up, you go back to Alice. You never draw two Bobs as the same person if he makes two separate calls. Each call is a new node.
    Go one level deeper
    The standard method breaks if you attempt to draw the tree based on source code layout rather than execution order. The tree must reflect the dynamic sequence of calls, meaning conditional branches that evaluate to false produce no child nodes. The tree is a record of actual events, not potential ones.

    Building the Tree for the Anchor Example

    Consider a program where main calls A(2). Function A(int n) calls B(), and then conditionally calls A(n-1) if n > 1. Function B() calls C(), and C() simply returns. Construct the activation tree for this execution.
    The immediate instinct is to draw main, then A, then B, then C, and then another A, all in a single flat list or merging the two A calls into one node because they share the same name.
    Merging the two A calls violates the definition of an activation tree, which requires a distinct node per invocation. Furthermore, placing them flat ignores the nesting. A(n-1) is called by A(2), so it must be a child of the A(2) node, not a sibling.
    1. Start with root node: main.
    2. main calls A(2). Add child node A(2) under main. Active node is A(2).
    3. A(2) calls B(). Add child node B() under A(2). Active node is B().
    4. B() calls C(). Add child node C() under B(). Active node is C().
    5. C() returns. Active node reverts to B().
    6. B() returns. Active node reverts to A(2).
    7. A(2) checks n > 1 (2 > 1 is true). It calls A(1). Add child node A(1) under A(2). Active node is A(1).
    8. A(1) calls B(). Add child node B() under A(1).
    9. This second B() calls C(). Add child node C() under the second B().
    10. C() returns, B() returns, A(1) checks n > 1 (1 > 1 is false), A(1) returns, A(2) returns, main returns.
    Verification: The tree has main at the root. A(2) is the child of main. A(2) has two children: the first B() and A(1). The first B() has child C(). A(1) has child B(), which in turn has child C().

    Drawing the Tree for Sequential Calls

    Given the following code snippet, which option correctly describes the structure of its activation tree?
    void X() { Y(); } void Y() { Z(); } void Z() {} int main() { X(); return 0; }
    TIME: 60s FORMAT: MCQ

    Runtime Environments and Procedure Calls: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Compiler Design MCQ

    Consider the following program:

    \begin{verbatim}

    void F(int n) {

    if (n > 1) {

    G();

    F(n - 1);

    F(n - 2);

    }

    }

    void G() { }

    \end{verbatim}

    \textbf{Assertion (A):} If calls , the total number of activation records for function is 5.

    \textbf{Reason (R):} The function is invoked exactly 5 times during this execution.

    Which of the following is correct?

    1. A.

      Both A and R are true and R is the correct explanation of A

    2. B.

      Both A and R are true but R is NOT the correct explanation of A

    3. C.

      A is true but R is false

    4. D.

      A is false but R is true

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: Activation records for a function = number of times that function is invoked.

    Step 1: Trace execution for :

    • : , so calls , ,
    • : , so calls , ,
    • from : , not , returns
    • from : , not , returns
    • from : , not , returns

    Step 2: Count invocations:

    • = 5 invocations

    Step 3: Evaluate A and R:

    • A: "activation records for is 5" = TRUE (5 invocations = 5 records)
    • R: " is invoked exactly 5 times" = TRUE
    • R explains A because activation records for = number of invocations.

    Answer: Both A and R are true and R is the correct explanation of A.

    Question 2 · Compiler Design MCQ

    Consider the following program:

    \begin{verbatim}

    void F(int n) {

    G();

    if (n > 0) {

    F(n - 1);

    }

    }

    void G() { }

    \end{verbatim}

    If calls , consider the following statements:

    \begin{itemize}

    \item : The activation tree contains exactly 3 nodes labeled .

    \item : The function is invoked exactly 2 times.

    \item : The maximum depth of the activation tree is 4.

    \end{itemize}

    Which of the following is correct?

    1. A.

      only

    2. B.

      only

    3. C.

      and only

    4. D.

      , , and

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: Trace the execution carefully, noting that is called before the condition check.

    Step 1: Trace execution for :

    • calls , then checks (true), calls
    • calls , then checks (true), calls
    • calls , then checks (false), returns

    Step 2: Evaluate each statement:

    • : nodes are . Count = 3. \textbf{TRUE}
    • : is called by . Count = 3, not 2. \textbf{FALSE}
    • : Deepest path: . Depth = 5, not 4. \textbf{FALSE}

    Answer: only

    Question 3 · Compiler Design MCQ

    Consider the following program:

    \begin{verbatim}

    void main() {

    P();

    }

    void P() {

    Q();

    R();

    }

    void Q() { }

    void R() { }

    \end{verbatim}

    Which of the following statements is TRUE about the execution of this program?

    1. A.

      When R() is executing, the active procedures are main, P, and R.

    2. B.

      When Q() is executing, the active procedures are main and Q.

    3. C.

      When P() is executing, the active procedures are main, P, Q, and R.

    4. D.

      When R() is executing, the active procedures are P and R.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: The active procedures at any point are exactly the procedures on the path from the root to the current node in the activation tree.

    Step 1: Build the activation tree.

    • main calls P
    • P calls Q
    • Q returns
    • P calls R
    • R returns
    • P returns
    • main returns

    The tree structure is:

    • main is the root
    • P is a child of main
    • Q is a child of P
    • R is a child of P

    Step 2: Identify active procedures at each point.

    • When Q is executing: path from root is main → P → Q. Active: main, P, Q
    • When R is executing: path from root is main → P → R. Active: main, P, R
    • When P is executing (between Q and R calls): path from root is main → P. Active: main, P

    Step 3: Check each option.

    • Option A: When R is executing, active are main, P, R. ✓ TRUE
    • Option B: When Q is executing, active are main, P, Q (not just main and Q). ✗ FALSE
    • Option C: When P is executing, active are main, P (not including Q and R, which haven't been called yet or have already returned). ✗ FALSE
    • Option D: When R is executing, active are main, P, R (not just P and R, main is also active). ✗ FALSE

    Answer: A

    Question 4 · Compiler Design NAT

    Consider the following program:

    \begin{verbatim}

    void main() {

    A();

    }

    void A() {

    B();

    C();

    }

    void B() {

    D();

    E();

    }

    void C() { }

    void D() {

    F();

    }

    void E() { }

    void F() { }

    \end{verbatim}

    When procedure F is executing, how many activation records are simultaneously present on the call stack?

    Correct Answer:

    5

    Step-by-Step Solution

    Key idea: This is an active procedures question. The number of activation records on the stack at any point is exactly the number of procedures on the path from the root to the current node in the activation tree.

    Step 1: Trace the execution path to procedure F.

    • main calls A.
    • A calls B.
    • B calls D.
    • D calls F.

    Step 2: Identify all active procedures at this exact moment.

    Because execution is strictly nested and single-threaded, every procedure that has been called on the way to F, but has not yet returned, is active.

    The active procedures are: main, A, B, D, and F.

    Step 3: Count the activation records.

    There are exactly 5 procedures on the root-to-node path.

    Answer: 5

    Question 5 · Compiler Design MCQ

    A program defines three functions: , , and . During execution, calls exactly twice. Each invocation of calls exactly three times. No other function calls occur. How many nodes does the activation tree for this execution contain?

    1. A.

      3

    2. B.

      6

    3. C.

      9

    4. D.

      12

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: An activation tree has one node per invocation, not per function name.

    Step 1: Count invocations: 1 node.

    Step 2: Count invocations: calls twice, so 2 nodes.

    Step 3: Count invocations: Each of the 2 invocations calls three times, so nodes.

    Step 4: Total nodes = .

    Answer: 9

    Question 6 · Compiler Design MCQ

    A program defines exactly four functions: , , , and . During a specific execution, calls three times, calls twice, and calls four times. No other calls occur. What is the number of nodes in the call graph for this program?

    1. A.

      4

    2. B.

      9

    3. C.

      10

    4. D.

      13

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: A call graph represents the static structure of the code, containing exactly one node per unique procedure defined, regardless of execution dynamics.

    Step 1: Identify all unique functions defined in the source code. The program defines , , , and .

    Step 2: Count the unique functions. There are exactly 4 unique functions.

    Step 3: Apply the definition of a call graph. The number of nodes equals the number of unique functions. The number of times they are called during execution (3, 2, 4) is irrelevant to the call graph's node count.

    Answer: 4

    Question 7 · Compiler Design MCQ

    Consider the following program:

    \begin{verbatim}

    void main() {

    P();

    Q();

    }

    void P() {

    R();

    R();

    R();

    }

    void Q() {

    S();

    }

    void R() {}

    void S() {}

    \end{verbatim}

    How many directed edges are present in the activation tree for this execution?

    1. A.

      4

    2. B.

      6

    3. C.

      7

    4. D.

      8

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: In an activation tree, every function invocation is a distinct node, and the number of directed edges is exactly one less than the total number of nodes.

    Step 1: Trace the execution to count the total number of nodes (invocations).

    • is the root: 1 node.
    • calls and : 2 nodes.
    • calls three times: 3 nodes.
    • calls once: 1 node.

    Total nodes = nodes.

    Step 2: Calculate the number of edges. In any tree, the number of edges is exactly one less than the number of nodes.

    Edges = Total nodes .

    Answer: 6

    Question 8 · Compiler Design MCQ

    Consider the following program:

    \begin{verbatim}

    void main() {

    X();

    Y();

    }

    void X() {

    Z();

    }

    void Y() { }

    void Z() { }

    \end{verbatim}

    What is the sequence of procedure returns during the execution of this program?

    1. A.

      main, X, Z, Y

    2. B.

      Z, X, Y, main

    3. C.

      X, Z, Y, main

    4. D.

      main, Z, X, Y

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: The sequence of procedure returns corresponds to a postorder traversal of the activation tree.

    Step 1: Build the activation tree by tracing execution.

    • main calls X (move down to X)
    • X calls Z (move down to Z)
    • Z returns (move back to X)
    • X returns (move back to main)
    • main calls Y (move down to Y)
    • Y returns (move back to main)
    • main returns

    Step 2: The return sequence follows postorder (children before parent).

    • Z returns first
    • Then X returns
    • Then Y returns
    • Finally main returns

    The return sequence is: Z, X, Y, main.

    Answer: B

    Question 9 · Compiler Design NAT

    Consider the following program:

    \begin{verbatim}

    void main() {

    A();

    }

    void A() {

    B();

    C();

    }

    void B() { }

    void C() { }

    \end{verbatim}

    In the sequence of procedure returns during the execution of this program, what is the position (1-indexed) of procedure C?

    Correct Answer:

    2

    Step-by-Step Solution

    Key idea: This is a return sequence question, recognizable because it asks for the order in which procedures finish execution. The sequence of procedure returns corresponds exactly to a postorder traversal of the activation tree.

    Step 1: Build the activation tree by tracing the execution top-down.

    • main calls A.
    • A calls B. B finishes and returns.
    • A then calls C. C finishes and returns.
    • A finishes and returns.
    • main finishes and returns.

    Step 2: Determine the return sequence (postorder traversal).

    The procedures return in the exact reverse order of their completion: B, then C, then A, then main.

    Step 3: Find the position of C in this sequence.

    The sequence is: 1st: B, 2nd: C, 3rd: A, 4th: main.

    Procedure C is at position 2.

    Answer: 2

    Question 10 · Compiler Design MSQ

    Consider the following program:

    \begin{verbatim}

    void main() {

    P();

    Q();

    }

    void P() { R(); }

    void Q() { S(); }

    void R() { }

    void S() { }

    \end{verbatim}

    Which of the following statements are TRUE regarding the execution of this program?

    1. A.

      In the sequence of procedure returns, R returns before P.

    2. B.

      In the sequence of procedure calls, R is called before P.

    3. C.

      In the sequence of procedure returns, P returns before S.

    4. D.

      In the sequence of procedure returns, Q returns before S.

    Correct Answer:

    ["A","C"]

    Step-by-Step Solution

    Key idea: This is a traversal mapping question. The sequence of procedure calls corresponds to a preorder traversal of the activation tree, while the sequence of procedure returns corresponds to a postorder traversal.

    Step 1: Build the activation tree by tracing the execution top-down.

    • main calls P, then Q.
    • P calls R.
    • Q calls S.

    The tree structure has main as the root, with children P and Q. P has child R, and Q has child S.

    Step 2: Determine the call sequence (preorder traversal).

    Preorder visits the root, then the left subtree, then the right subtree.

    Call sequence: main, P, R, Q, S.

    Step 3: Determine the return sequence (postorder traversal).

    Postorder visits the left subtree, then the right subtree, then the root.

    Return sequence: R, P, S, Q, main.

    Step 4: Evaluate each option against these sequences.

    • Option A: In returns, R is before P. (True, R is 1st, P is 2nd).
    • Option B: In calls, R is before P. (False, P is 2nd, R is 3rd).
    • Option C: In returns, P is before S. (True, P is 2nd, S is 3rd).
    • Option D: In returns, Q is before S. (False, S is 3rd, Q is 4th).

    Answer: A, C

    More notes in this unit