Skip to main content

GATE CSE 2021Compiler Design › Runtime Environments & Parameter PassingVerified

GATE CSE 2021 PYQ: Consider the following statements. S 1 : S_1: S 1 ​ : The sequence of procedure calls corresponds to a…

Consider the following statements.

  • S1:S_1: The sequence of procedure calls corresponds to a preorder traversal of the activation tree.
  • S2:S_2: The sequence of procedure returns corresponds to a postorder traversal of the activation tree.

Which one of the following options is correct?

  1. A
    S1S_1 is true and S2S_2 is false
  2. B
    S1S_1 is false and S2S_2 is true
  3. C
    S1S_1 is true and S2S_2 is true
    Correct answer
  4. D
    S1S_1 is false and S2S_2 is false

Answer

(C) S 1 S_1 S 1 ​ is true and S 2 S_2 S 2 ​ is true

Approach

Model the program execution as an activation tree: each procedure invocation is a node, and when a procedure calls another, the callee becomes a child of the caller. Then compare the chronological order of calls and returns against tree traversal orders.

In the activation tree, a node is 'entered' (called) before its children execute and 'exited' (returned) after all children have exited. The first pattern is preorder and the second is postorder, so both S1 and S2 are true, giving option C.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.