Consider the following program, writ ten in JavaScript-like syntax: // main prog
ID: 3738180 • Letter: C
Question
Consider the following program, writ ten in JavaScript-like syntax: // main prograrm var x, Y. 2 function subl) var a,yz function sub2 ) var a, b, z; function sub3 ) vara, x, w; Given the following calling sequences and assuming that dynamic scop- ing is used, what variables are visible during execution of the last subpro- gram activated? Include with each visible variable the name of the unit where it is declared. a. main calls subl; subl calls sub2; sub2 calls sub3 b. main calls subl; subl calls sub3 c. main calls sub2; sub2 calls sub3; sub3 calls sub1. d. main calls sub3; sub3 calls subl. e. main calls subl; subl calls sub3; sub3 calls sub2 f. main calls sub3; sub3 calls sub2; sub2 calls sub1.Explanation / Answer
Answer
Given that,
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;
. . .
}
program:-
Consider the given data:
// main program
var x, y, z;
function subl ( )
{
var a, y, z;
. . .
}
function sub2 ( )
{
var a, b, z;
• • •
}
function sub3 ( )
{
var a, x, w;
• • •
}
a) Consider the sequence: "main calls subl; subl calls sub2 ; sub2 calls sub3-
Function call Variables
subl y
sub2 b,z
sub3 a,x,w
b) Consider the sequence: "main calls subl ; subl calls sub3"
Function call Variables
sub1 y,z
sub3 a,x,w
c) Consider the sequence: "main calls sub2; sub2 calls sub3 ; sub3 calls subl"
Function call variables
Sub2 b
sub3 x,w
subl a,y,z
d)Consider the sequence: "main calls sub3; sub3 calls subl"
Function call Variables
sub3 x,w
subl a,y,z
e) Consider the sequence: "main calls subl; subl calls sub3; sub3 calls sub2"
Function call Variables
subl y
sub3 x,w
sub2 a,b,z
d) Consider the sequence: "main calls sub3; sub3 calls sub2; sub2 calls subl"
Function call Variables
sub3 x,w
sub2 b
subl a,y,z
[ for reference:- a. main calls sub1; sub1 calls sub2; sub2 calls sub3.
Answer: a x w in sub3. b, z in sub2, y in sub1.
b. main calls sub1; sub1 calls sub3.
Answer:a x w in sub3, y z in sub1.
c. main calls sub2; sub2 calls sub3; sub3 calls sub1.
Answer: a y z in sub1, x w in sub3, b in sub2.
d. main calls sub3; sub3 calls sub1.
Answer: a y z in sub1; x w in sub3.
e. main calls sub1; sub1 calls sub3; sub3 calls sub2.
Answer: a b z in sub2, x w in sub3; y in sub1.
f. main calls sub3; sub3 calls sub2; sub2 calls sub1.
Answer: a y z in sub1; b in sub2; x w in sub3
function sub2 ) var a, b, z function sub3 ) vara, x, w; ]