Consider the following statements.
- S1: The sequence of procedure calls corresponds to a preorder traversal of the activation tree.
- S2: The sequence of procedure returns corresponds to a postorder traversal of the activation tree.
Which one of the following options is correct?
(A) S1 is true and S2 is false
(B) S1 is false and S2 is true
(C) S1 is true and S2 is true
(D) S1 is false and S2 is false
Answer: (C)
Explanation: The use of a run-time stack is enabled by several useful relationships between the activation tree and the behavior of the program:
- The sequence of procedure calls corresponds to a preorder traversal of the activation tree.
- The sequence of returns corresponds to a postorder traversal of the activation tree.
- The path from the root to a node N shows the activations that are live at the time N is executing.
- Procedure calls and returns are managed by a control stack, i.e., run-time stack.
On each procedure call, an activation record for that procedure is pushed on the stack. When the call returns, that activation record is popped from the stack.
The order in which these activations were called is the order in which they appear along the path to N, starting at the root, and they will return in the reverse of that order.
Quiz of this Question