0% found this document useful (0 votes)
34 views2 pages

Homework #2

1. Homework #2 asks students to answer 4 questions in writing about JavaScript scoping rules, C memory allocation issues, order of operations in expressions, and the evaluation order of function calls. 2. Students are to submit their written answers to these questions by May 06, 2019. 3. The questions cover: a) variable scoping in JavaScript, b) memory allocation problems in a C code segment, c) using precedence and associativity to determine evaluation order of expressions, and d) how evaluation order impacts the result of adding function calls that modify a variable.

Uploaded by

Atilla ATEŞ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Homework #2

1. Homework #2 asks students to answer 4 questions in writing about JavaScript scoping rules, C memory allocation issues, order of operations in expressions, and the evaluation order of function calls. 2. Students are to submit their written answers to these questions by May 06, 2019. 3. The questions cover: a) variable scoping in JavaScript, b) memory allocation problems in a C code segment, c) using precedence and associativity to determine evaluation order of expressions, and d) how evaluation order impacts the result of adding function calls that modify a variable.

Uploaded by

Atilla ATEŞ
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

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.

You might also like