Wednesday, October 29, 2014

Q&A Assignment #7 Chapter 5 Review Questions & Problem Set

Name : Vina Melinda
NIM  : 1801380106

Untuk kali ini saya akan menjawab Assignment #7 Problem Set dan Review Questions dari Chapter 5 dari Sebesta.


Review Questions:

6. Q: What is the l-value of a variable? What is the r-value?
A: An l-value represents a storage region’s “locator” value, or a “left” value, implying that it can appear on the left of the equal sign (=). L-values are often identifiers. The term “r-value” is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are r-values but not all r-values are l-values.

7. Q: Define binding and binding time.
A: Binding is an association between an attribute and an entity, such as between a variable and its type of value, or between an operation and a symbol. Binding time is the time at which binding takes place. Binding and binding times are prominent concepts in semantics of programming language.

8. Q: After language design and implementation [what are the four times bindings can take place in a program?]
A: compile time: a variable in java bound to a particular data type, load time: a variable bound to a storage cell when a program is loaded into memory. link time: a call to a library subprogram is bound to the subprogram code. run time: certain variables declared in pascal and in c++ functions.

9. Q: Define static binding and dynamic binding.
A: Static binding: if it first occurs before run time and remains unchanged throughout program execution.

Dynamic binding: if it first occurs during execution or can change during execution of the program.

10. Q: What are the advantages and disadvantages of implicit declarations?
A: Implicit declarationscan be detrimental to reliability because they prevent the compilation process from detecting some typographical and programmer errors.


Problem Set:



6. Q: 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
ii. sub2
iii. sub3
b. Repeat part a, but assume dynamic scoping.

A: a.) i.) sub1
ii.) sub1
iii.) Main

b.) i.) sub1
ii.) sub1
iii.) sub1

7. Q: 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();

A: Static Scope then x = 5, if Dynamic Scope then x = 10

8. A: 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.

A: Sub1: a(sub1), y(sub1), z(sub1), x(main).
Sub2: a(sub2), b(sub2), z(sub2), y(sub1), x(main)
Sub3: a(sub3), x(sub3), w(sub3), y(main), z(main)
9. Q: 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.

A: 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

10. Q: 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 four marked points in this function, list each visible variable,
along with the number of the definition statement that defines it.

A: 1) Variables : a = 1 b = 2
c = 2 d = 2

2) Variables :
a = 1 b = 2
c = 3 d = 3
e = 3

3) Variables :
a = 1 b = 2
c = 2 d = 2

4) Variables :
a = 1
b = 1
c = 1

No comments:

Post a Comment