Q6 : Consider the following JavaScript skeletal program:
// The main
program
var x;
function sub1() {
var x;
function sub2() {
. . .
}
}
function sub3() {
. . .
}
Assume that the execution of this program is in the following unit order:
main calls sub1
sub1 calls sub2
sub2 calls sub3
a. Assuming static scoping, in the following, which declaration
of x is the correct one for a reference to x?
i. sub1
ii. sub2
iii. sub3
b. Repeat part a, but assume dynamic scoping.
var x;
function sub1() {
var x;
function sub2() {
. . .
}
}
function sub3() {
. . .
}
Assume that the execution of this program is in the following unit order:
main calls sub1
sub1 calls sub2
sub2 calls sub3
a. Assuming static scoping, in the following, which declaration
of x is the correct one for a reference to x?
i. sub1
ii. sub2
iii. sub3
b. Repeat part a, but assume dynamic scoping.
A :
a. In sub1:
sub1
In sub2: sub1
In sub3: main
In sub2: sub1
In sub3: main
b.In sub1:
sub1
In sub2: sub1
In sub3: sub1.
In sub2: sub1
In sub3: sub1.
Q7 : Assume the following JavaScript program was interpreted using
static-scoping rules. What value of x is displayed in function sub1? Under
dynamic-scoping rules, what value of x is displayed in function sub1?
var x; function sub1() {
document.write("x = " + x +
"<br />");
}
function sub2() {
var x;
x = 10;
sub1();
}
x = 5;
sub2();
A :
Static scope: x=5
Dynamic scope: x=10
Q8 : Consider the following JavaScript program:
var x, y, z; function sub1() {
var a, y, z;
function sub2() {
var a, b, z;
. . .
}
. . .
}
function sub3() {
var a, x, w;
. . .
}
List all the variables, along with the program units
where they are declared, that are visible in the bodies of sub1, sub2, and
sub3, assum- ing static scoping is used.
A :
sub1: a(sub1)
y(sub1) z(sub1) x(main)
sub2: a(sub2) b(sub2) z(sub2) y(sub1) x(main)
sub3: a(sub3) x(sub3) w(sub3) y(main) z(main)
sub2: a(sub2) b(sub2) z(sub2) y(sub1) x(main)
sub3: a(sub3) x(sub3) w(sub3) y(main) z(main)
Q9 : Consider the following Python program:
x = 1; y = 3;
z = 5;
def sub1():
a = 7;
y = 9;
z = 11;
. . .
def sub2():
global x;
a = 13;
x = 15;
w = 17;
. . .
def sub3():
nonlocal a;
a = 19;
b = 21;
z = 23;
. . .
. . .
List all the variables, along with the program units
where they are declared, that are visible in the bodies of sub1, sub2, and
sub3, assum- ing static scoping is used.
A :
Variable Where
Declared
In sub1:
a
sub1
y
sub1
z
sub1
x
main
In sub2:
a
sub2
x
sub2
w
sub2
y
main
z
main
In sub3:
a
sub3
b
sub3
z
sub3
w
sub2
x
sub2
y
main
Q10 : Consider the following C program:
void fun(void) { int a, b, c; /* definition 1 */
. . .
while (. . .) {
int b, c, d; /*definition 2
*/
. . . à1
while (. . .) {
int c, d, e; /* definition 3 */
. . . à 2
}
. . . -à3
}
. . . à4
}
For each of the four marked points in this function,
list each visible vari- able, along with the number of the definition statement
that defines it.
A :
Point 1: a:1 b:2 c:2 d:2
Point 2: a:1 b:2 c:3 d:3 e:3
Point 3: a:1 b:2 c:2 d:2
Point 4: a:1 b:1 c:1
No comments:
Post a Comment