GATE 2016 CS (CS1) – Question 46
What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?
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);}Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (D) 4, 4
Explanation
The historical key selects **D: 4,4**. This requires interpreting the first assignment inside m as introducing a local a, while y still refers to the outer a=3. Then local a becomes $3-1=2$, and n(a) squares that referenced local value to 4; both prints show 4.
The pseudocode does not explicitly declare that local variable. If the assignment instead updates the existing dynamically visible outer a, reference parameter y aliases the same cell: a=1 makes y=1, a=y-a becomes 0, and n prints 0 followed by m printing 0.
Thus 4,4 is the retained scoring answer under the intended local-binding interpretation; 0,0 follows under the outer-binding interpretation. Parameter passing must not be incorrectly described as copying y by value.