chapter

    Runtime Environments and Procedure Calls Short Notes for Gate Cs

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

    Runtime Environments and Procedure Calls short notes

    Formula matrix

    Calls = PreorderSingle-threaded, synchronous execution
    Returns = PostorderCaller waits for callee to finish
    Active procedures = Root-to-node pathStandard single call stack model
    Max stack depth = Tree depthCounting root as depth 1
    Node count = Total invocationsDistinct node per call, not per name

    If you see this, do this

    "sequence of procedure calls"Apply preorder traversal.
    "sequence of procedure returns"Apply postorder traversal.
    "active procedures at point X"Trace path from root to node X.
    "maximum stack depth"Find maximum depth of the activation tree.
    "distinct activation records for F"Count all nodes labeled F in the tree.
    "activation tree for code snippet"Simulate execution top-down, new node per call.
    "left-to-right evaluation of operands"Call leftmost function first, ignore precedence.

    Traps and invariants

    • Conflating call graph with activation tree. Check: Verify if diagram has one node per unique procedure (graph) or one per invocation (tree).
    • Reversing traversal order for calls and returns. Check: Calls happen on entry (preorder), returns happen on exit (postorder).
    • Mishandling recursive calls as a single node. Check: Every distinct invocation, including recursive ones, must be a separate node.

    1 more card 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 short 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 Short Notes for GATE CS

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

    Formula matrix

    Calls = PreorderSingle-threaded, synchronous execution
    Returns = PostorderCaller waits for callee to finish
    Active procedures = Root-to-node pathStandard single call stack model
    Max stack depth = Tree depthCounting root as depth 1
    Node count = Total invocationsDistinct node per call, not per name

    If you see this, do this

    "sequence of procedure calls"Apply preorder traversal.
    "sequence of procedure returns"Apply postorder traversal.
    "active procedures at point X"Trace path from root to node X.
    "maximum stack depth"Find maximum depth of the activation tree.
    "distinct activation records for F"Count all nodes labeled F in the tree.
    "activation tree for code snippet"Simulate execution top-down, new node per call.
    "left-to-right evaluation of operands"Call leftmost function first, ignore precedence.

    Traps and invariants

    • Conflating call graph with activation tree. Check: Verify if diagram has one node per unique procedure (graph) or one per invocation (tree).
    • Reversing traversal order for calls and returns. Check: Calls happen on entry (preorder), returns happen on exit (postorder).
    • Mishandling recursive calls as a single node. Check: Every distinct invocation, including recursive ones, must be a separate node.

    Recall check

    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 short notes in this unit