GATE / CS ARCHIVE

GATE CSE 2001 / QUESTION 2.19

Reference parameters with dynamic scope

Compiler Design · Runtime Environments & Parameter Passing · Full GATE CSE 2001 paper

GATE 2001, Q2.19 2 marks · MCQ
Report issue

Runtime Environments & Parameter Passing

Question 2.19, GATE 2001: Program P2 has outer n=10. Procedure D declares local n=3 and calls W(n). W takes x by reference, increments x, and prints x. Under dynamic scoping, choose output 10,11,3, or none.
Tap to enlarge
Your answer
Answer & solution

Answer: D; the value printed is 4 (no official key was published)

Step 1: The outer n is set to 10, but procedure D declares its own n and sets it to 3. At the call W(n), the actual argument is that active local variable.

Step 2: Reference passing binds W’s x to D’s n. Executing x=x+1 changes it from 3 to 4; print x therefore prints 4. The outer n remains 10.

Since the listed numeric choices are 10,11,3, the answer is none of these, D. The formal binding already identifies the object, so dynamic scope does not redirect x to the outer n.

Original paper, page 6