GATE / CS ARCHIVE

GATE CSE 2021 SET 1 / QUESTION 4

GATE CSE 2021 Set 1 Q4

Compiler Design · Runtime Environments & Parameter Passing · Full GATE CSE 2021 Set 1 paper

GATE 2021 Set 1, Q4 1 mark · MCQ
Report issue

Runtime Environments & Parameter Passing

Question 4, GATE 2021: S1: The sequence of procedure calls is a preorder traversal of the activation tree. S2: The sequence of procedure returns is a postorder traversal. Which is correct? A S1 true/S2 false; B S1 false/S2 true; C both true; D both false.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

A procedure activation is entered before its child calls, producing preorder. It returns only after its nested calls finish, producing postorder. Thus both statements are true, C, in the ordinary sequential, properly nested call-return model.

For example, if main calls A, A calls B and C in order, and main then calls D, the entry order is main,A,B,C,D. The completion order is B,C,A,D,main. These are exactly preorder and postorder on the ordered activation tree. Recursive calls create distinct activation nodes even when the procedure name repeats; one must not merge all calls of the same procedure into a single node.

Original paper, page 13 · Answer key