Consider the following program, written in Javascript-like syntax://main program
ID: 3836289 • Letter: C
Question
Consider the following program, written in Javascript-like syntax://main program var x, y, z; function sub1() {var a, y, z; ...} function sub2() {var a, b, z; ...} function sub3() {var a, x, w; ...} Given the following sequence main calls sub3; sub3 calls sub2; sub2 calls sub1. and assuming that dynamic scoping is used, what variables are visible during execution of the last subprogram activated? Include with each visible variable the name of the unit (module, sub-function) where it is declared. [ANSWER]Explanation / Answer
The variables are visible in last subporgram execute, i.e., in sub1() are
a, y, z -> Declared in sub1
b -> Declared in sub2
x, w -> Declared in sub3
Explanation
main calls sub3
sub3 calls sub2
sub2 calls sub1
1. When main calls sub3, the variable x in sub3 over-shadows the global variable x. So, the variables that are visible in sub3() are
a, w, x -> From sub3
y, z -> From main
2. Now sub3 calls sub2, and since the scoping is dynamic, sub2 inherits the scope of sub3. The variable in a, in sub2 overshadows variable a from sub3 and similarly, variable z in sub2 overshadows variable z from the main. The variables that are visible in sub2 are
a, b, z -> From sub2
x, w -> From sub3
y -> From main
3. Now sub2 calls sub1, and sub1 inherits the scope of sub2. The variable in a, y, z in sub1 overshadows variable a parent scope, sub2. The variables that are visible in sub1 are
a, y, z -> From sub1
b -> From sub2
x, w -> From sub3