In: Computer Science
QUESTION 1
Consider the following program:
var x = 0;
function sub1() {
var x = 1;
function sub2() {
function sub3() {
print
x;
}
sub3();
}
function sub4() {
var x = 2;
sub2();
}
sub4();
}
sub1();
If this code uses static scoping, when it is executed what is
printed?
0 |
||
1 |
||
2 |
QUESTION 2
Consider the following program:
var x = 0;
function sub1() {
var x = 1;
function sub2() {
function sub3() {
print
x;
}
sub3();
}
function sub4() {
var x = 2;
sub2();
}
sub4();
}
sub1();
If this code uses dynamic scoping, when it is executed what is
printed?
0 |
||
1 |
||
2 |
QUESTION 3
Consider the following program:
void fun1 (void);
void fun2 (void);
void fun2 (void);
void main() {
int a, b, c;
}
void fun1() {
int b, c, d;
}
void fun2() {
int c, d, e;
}
void fun3() {
int d, e, f;
}
and the following call sequence: main calls fun2; fun2 calls fun1;
fun1 calls fun3.
Assuming dynamic scoping, what are the variables visible within
fun3?
d, e, and f from fun3 |
||
d, e, and f from fun3; a, b, anc c from main |
||
d, e, and f from fun3; b and c from fun 1; a from main |
QUESTION 4
Consider the following program:
void fun1 (void);
void fun2 (void);
void fun2 (void);
void main() {
int a, b, c;
}
void fun1() {
int b, c, d;
}
void fun2() {
int c, d, e;
}
void fun3() {
int d, e, f;
}
and the following call sequence: main calls fun1; fun1 calls fun3;
fun3 calls fun2.
Assuming dynamic scoping, what are the variables visible within
fun2?
a, b , and c from main; d from fun 1; e and f from fun3 |
||
a from main; b from fun1; f from fun 3; c, d, and e from fun2 |
||
c, d, and e from fun2 |