GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 9(b.ii)

Dynamic scope selects the active caller

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

GATE 1991, Q9(b.ii) Descriptive
Report issue

Runtime Environments & Parameter Passing

Question 9(b.ii), GATE 1991: Global integers a,b; procedure P sets a:=5;b:=10. Procedure Q declares its own local integers a,b and calls P. Main sets global a:=1;b:=2, calls Q, then prints global a,b. Give output under dynamic scope.
Question 9(b.ii), GATE 1991: Global integers a,b; procedure P sets a:=5;b:=10. Procedure Q declares its own local integers a,b and calls P. Main sets global a:=1;b:=2, calls Q, then prints global a,b. Give output under dynamic scope.
Tap to enlarge
Answer & solution

Answer: a=1,b=2. (no official key was published)

Dynamic scope resolves a free name by searching the active call chain from the current activation outward. During P, the chain is P→Q→main/global. P has no local a or b; the nearest active bindings are Q’s local a,b. Hence P’s assignments set Q’s variables to (5,10), rather than modifying the globals.

The globals were initialized to (1,2) by main and are still (1,2) when Q returns. Q’s locals disappear with its activation, and the final print in main uses the globals. The output is a=1,b=2.

No undefined read is caused by Q’s initially uninitialized locals: P overwrites them before they could be read, and the final print occurs after they are out of scope. Dynamic scoping must not be confused with dynamic memory allocation or call-by-reference parameter passing. It is a rule for selecting which binding an identifier denotes; the assignment operation itself is unchanged.

Original paper, page 6