for i := j + 1 step i * j until 3*j do j := j + 1The semantics should be clear, but you can refer to page 327 of the book for all details. Assume that the initial value of j is 1. List the sequence of values taken by variable i, assuming the following semantics:
a. (10 points) All expressions are evaluated once at the loop entryIn all cases, when more than one expression is evaluated at the same time, they are evaluated in left-to-right order. Also, the assignment is always done as soon as its RHS (Right-Hand-Side) is evaluated.
i = 2
i = 4
b. (10 points) All expressions are evaluated before each iteration
i = 2
i = 4
i = 12
c. (10 points) step expressions are evaluated once at loop entry, and until expressions are evaluated before each iteration
i = 2
i = 4
i = 6
i = 8
...
d. (10 points) until expressions are evaluated once at loop entry, and step expressions are evaluated before each iteration, just after the loop counter is incremented
i = 2
i = 4
void main() {For each of the following parameter-passing methods, what is the output of the program:
int value = 2, list[5] = {1,3,4,7,9};
swap(value,list[0]);
printf("%d and [%d,%d,%d,%d,%d]\n",
value,list[0],list[1],list[2],list[3],list[4]);
swap(list[0],list[1]);
printf("%d and [%d,%d,%d,%d,%d]\n",
value,list[0],list[1],list[2],list[3],list[4]);
swap(value,list[value]);
printf("%d and [%d,%d,%d,%d,%d]\n",
value,list[0],list[1],list[2],list[3],list[4]);
return 0;
}
void swap(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}
a. (10 pts) Passed by value.
2 and [1,3,4,7,9]
2 and [1,3,4,7,9]
2 and [1,3,4,7,9]
b. (10 pts) Passed by reference.
1 and [2,3,4,7,9]
1 and [3,2,4,7,9]
2 and [3,1,4,7,9]
c. (10 pts) Passed by name.
1 [2,3,4,7,9]
1 [3,2,4,7,9]
2 [3,2,1,7,9]
d. (10 pts) Passed by value-result.
1 and [2,3,4,7,9]
1 and [3,2,4,7,9]
2 and [3,1,4,7,9]
int x;a. (20 points) What does this program print if the language uses static scoping?
void set_x(int n) {
x = n;
}
void print_x() {
printf("%d\n",x);
}
void first() {
set_x(1);
print_x();
}
void second() {
int x;
set_x(2);
print_x();
}
int main() {
set_x(0);
first();
print_x();
second();
print_x();
}
1
1
2
2
b. (20 points) What does this program print if the language uses dynamic
scoping?
1
1
2
1
int x;Assume that the language uses dynamic scoping.
void set_x(int n) {
x = n;
}
void print_x() {
printf("%d\n",x);
}
void foo(void(S*)(int m), void(P*)(void), int n) {
int x = 0;
if ((n == 1) || (n == 3))
set_x(n);
else
S(n);
if ((n == 1) || (n == 2))
print_x;
else
P();
}
int main() {
set_x(0); foo(set_x,print_x,1); print_x();
set_x(0); foo(set_x,print_x,2); print_x();
set_x(0); foo(set_x,print_x,3); print_x();
set_x(0); foo(set_x,print_x,4); print_x();
}
a. (20 points) What does the program print if the language uses shallow binding?
Under shallow binding all called to print_x and set_x use dynamic scoping
1
0
2
0
3
0
4
0
b. (20 points) What does the program print if the language uses deep binding?
Under deep binding the called to print_x and set_x as P() and S() use static scoping.
1
0
0
2
0
0
4
4