GATE / CS ARCHIVE

GATE CSE 2016 SET 1 / QUESTION 36

GATE CSE 2016 Set 1 Q36

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

GATE 2016 Set 1, Q36 2 marks · MCQ
Report issue

Runtime Environments & Parameter Passing

Question 36, GATE 2016: Assume dynamic scoping and pass-by-reference in this pseudocode:
a=3;
void n(x){x=x*a;print(x);}
void m(y){a=1;a=y−a;n(a);print(a);}
void main(){m(a);}
What is printed? A:6,2. B:6,6. C:4,2. D:4,4. The source does not explicitly declare whether m's first assignment introduces a local a; preserve that ambiguity.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Our answer: D under implicit local-a binding

The intended pseudocode treats a=1 inside m as introducing a local a, while reference parameter y remains an alias of the outer a=3. Then local a=y−a=2. Passing it by reference to n makes x and dynamically resolved a refer to that same local cell: x=x*a stores 4, and both prints show 4, D. If a=1 instead assigns the existing outer dynamically scoped a, y aliases it, so a=y−a becomes 0 and the output is 0, 0. The source lacks an explicit local declaration, so its binding convention must be supplied.

Distinguish parameter aliasing from name lookup. Under the intended local-binding interpretation, y aliases the global cell containing 3, while m's local a first contains 1 and then 2. n's formal x aliases that local cell, and dynamic lookup of its free name a finds the active m frame, also that same cell. Thus the assignment squares 2 in place. Under assignment-to-existing-binding semantics both y and a refer to the global cell before subtraction, making 1−1=0. The printed key chooses the former semantics but does not remove the source ambiguity.

Original paper, page 15 · Answer key