Harmony Clean Flat Responsive WordPress Blog Theme

Assignment 5

Review Questions
1. What is the l-value of a variable? What is the r-value?
The l-value of variable is the address of a variable because the address is required when the name of a variable appears in the left side of an assignment. While r-value of variable is a variable’s value because it is required when the name of the variable appears in the right side of an assignment statement.
2. Define binding and binding time.
A binding is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol. While the time at which a binding takes place is called binding time.
3. After language design and implementation [what are the four times bindings can take place in a program?
First the compile time binding, second the load time binding, third the link time binding and lastly the run time binding.
4. Define static binding and dynamic binding
A static binding is when the binding occurs before run time begins and remains unchanged throughout program execution. If the binding first occurs during run time or can change in the course of program execution, it is called dynamic binding.
5. What are the advantages and disadvantages of implicit declarations?
The advantage of implicit declarations is that Simple in naming conventions. In this case, the compiler or interpreter binds a variable to a type based on the syntactic form of the variable’s name. While the disadvantage of implicit declarations is it can be detrimental to reliability because they prevent the compilation process from detecting some typographical and programmer errors.
Problem Sets
1. Consider the following JavaScript skeletal program:
// The main program
var x;
function sub1() {
var x;
function sub2() {
}
}
function sub3() {
}
Assume that the execution of this program is in the following unit order:
main calls sub1
sub1 calls sub2
sub2 calls sub3
a. Assuming static scoping, in the following, which declaration of x is the correct one for a reference to x?
i. sub1
sub1: sub1
ii. sub2
sub2: sub1
iii. sub3
sub3: main
b. Repeat part a, but assume dynamic scoping.
i. sub1
sub1: sub1
ii. sub2
sub2: sub1
iii. sub3
sub3: sub1
2. 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 = 10;
sub1();
}
x = 5;
sub2();
static scoping rule = 5
dynamic scoping rule = 10
3. Consider the following JavaScript program:
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.
Variable                       Where Declared
In Sub1:
a                                             Sub1
y                                             Sub1
z                                             Sub1
x                                             Main
In Sub2:
a                                             Sub2
b                                             Sub2
z                                             Sub2
y                                             Sub1
x                                             Main
In Sub3:
a                                             Sub3
x                                             Sub3
w                                            Sub3
y                                             Main
z                                             Main
4. Consider the following Python program:
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, assuming static scoping is used.
Variable                       Where Declared
In Sub1:
a                                              Sub1
y                                              Sub1
z                                              Sub1
x                                              Main
In Sub2:
a                                              Sub2
x                                              Sub2
w                                             Sub2
y                                              Main
z                                              Main
In Sub3:
a                                              Sub3
b                                              Sub3
z                                              Sub3
w                                             Sub2
x                                              Sub2
y                                              Main
5. Consider the following C program
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 4 marked points, list all visible variables, along with the number of the definition statement that defines it.
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