Posted by : Andreas Kwan Rabu, 29 Oktober 2014

7. 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 ?
Answer :
var x;
function sub1() {
document.write(“x = ” + x + “<br />”);
}
function sub2() {
var x;
x = 10;
sub1();
}
x = 5;
sub2();
Answer:
Static scope: x=5, Dynamic scoping: x=10.
8. Consider the following JavaScript program:
Answer :
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, assuming static scoping is used.
Answer:
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)
9. Consider the following Python program:
Answer :
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, assumingstatic scoping is used.
Answer:
point 1 :  x = 1(main), y = 9 (sub1), z = 11(sub1) ,a = 7(sub1);
point 2 :  x =15(sub2), w = 17(sub2), a = 13(sub2), y = 9(sub1);
point 3 :  x = 15(sub2), b = 21(sub3), a = 19(sub1), z = 23(sub3), w = 17(sub 2);
point 4 :  x = 15(sub2), b = 21(sub3), a = 19(sub1), z = 23(sub3), w = 17(sub 2);
10. Consider the following C program:
Answer :
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 variable,
along with the number of the definition statement that defines it.
Answer:
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
11. Consider the following skeletal C program:
Answer :
void fun1(void); /* prototype */
void fun2(void); /* prototype */
void fun3(void); /* prototype */
void main() {
int a, b, c;
. . .
}
void fun1(void) {
int b, c, d;
. . .
}
void fun2(void) {
int c, d, e;
. . .
}
void fun3(void) {
int d, e, f;
. . .
}
Given the following calling sequences and assuming that dynamic scoping
is used, what variables are visible during execution of the last function
called? Include with each visible variable the name of the function in
which it was defined.
a. main calls fun1; fun1 calls fun2; fun2 calls fun3.Answer:
var a = main ; var b = fun1 ; var c = fun2 ;var d,e,f = fun3
b. main calls fun1; fun1 calls fun3.Answer:
var a = main; var b,c = fun1; var d,e,f = fun3
c. main calls fun2; fun2 calls fun3; fun3 calls fun1.
Answer:
var a= main; var b,c,d = fun1 ;var e,f = fun3
d. main calls fun3; fun3 calls fun1.
Answer:
var a = main; var b,c,d = fun1; var e,f = fun3
e. main calls fun1; fun1 calls fun3; fun3 calls fun2.
Answer:
var a=main;var c,d,e=fun2; var b =fun1; var f= fun3
f. main calls fun3; fun3 calls fun2; fun2 calls fun1.
Answer:
var a=main; var b,c,d = fun1; var f= fun3;var e=fun2
12. Consider the following program, written in JavaScript-like syntax:
// main program
var x, y, z;
Answer :
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.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.

{ 1 komentar... read them below or add one }

Welcome to My Blog

Mengenai Saya

Foto saya
Hanyalah salah satu manusia yang hidup di dunia ini

Popular Post

Blogger templates

Diberdayakan oleh Blogger.

Total Tayangan Halaman

- Copyright © BlogOfCommoners -Robotic Notes- Powered by Blogger - Designed by Johanes Djogan -