Homework #2
Answer the following questions. Use your own words while answering the questions. Write
your answers on a paper and submit your solutions to me during the class hour on May 06,
2019.
1. 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 = 20;
sub1();
}
x = 10;
sub2();
2. Explain the problems given in the below C code segment when it is executed.
int Sum[100], *p1, *p2, *p3;
p1 = (int *)malloc(sizeof(int)*200);
p2 = p1;
free(p2);
p3 = (int *)malloc(sizeof(int)*300);
p3 = Sum;
3. Assume the following rules of associativity and precedence are defined for expressions:
Precedence Highest *, /, not
+, – , &, mod
Unary –
== , != , < , <= , > , >=
and
Lowest or, xor
Associativity Left-to-right
Show the order of evaluation of the following expressions by parenthesizing all
subexpressions and placing a superscript or subscript on the right parenthesis to
indicate the order.
a mod b * 7 + d
a * b + x == c + d mod 5 and x != y
-a + b or c == d & f
4. Consider the following C program:
int fun(int *i){
*i += 2;
return 10;
}
void main(){
int x = 3;
x = x + fun(&x) + x + fun(&x);
}
What is the value of x after the assignment statement in main, assuming
a) operands are evaluated left to right.
b) operands are evaluated right to left.