Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following program, written in JavaScript-like syntax: // main progr

ID: 3539101 • 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 calling sequences 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 where it is declared.

a.       main calls sub1; sub1 calls sub2; sub2 calls sub3.

b.      main calls sub1; sub1 calls sub3.

c.       main calls sub2; sub2 calls sub3; sub3 calls sub1.

d.      main calls sub3; sub3 calls sub1.

e.       main calls sub1; sub1 calls sub3; sub3 calls sub2.

f.       main calls sub3; sub3 calls sub2; sub2 calls sub1.

Explanation / Answer

// 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 calling sequences and assuming dynamic scoping, what variables are visible during execution of the last subprogram activates? Include with each visible variable the name of the unit where it is declared. 12a. main calls sub1; sub1 calls sub2; sub2 calls sub3. ANS: a x w in sub3. b,z in sub2, y in sub1. 12b. main calls sub1; sub1 calls sub3. ANS: a x w in sub3, y z in sub1. 12c. main calls sub2; sub2 calls sub3; sub3 calls sub1. ANS: a y z in sub1, x w in sub3, b in sub2. 12d. main calls sub3; sub3 calls sub1. ANS: a y z in sub1; x w in sub3; 12e. main calls sub1; sub1 calls sub3; sub3 calls sub2. ANS: a b z in sub2, x w in sub3; y in sub1. 12f. main calls sub3; sub3 calls sub2; sub2 calls sub1. ANS: a y z in sub1; b in sub2; x w in sub3.