Sebesta Chapter 5 selected questions solutions

Q1. What is the potential danger of case-sensitive names?

The potential danger is a serious detriment to readability, because names that look very similar in fact denote different entities. Case-sensitive violates the design principle that language constructs that look similar should have similar meanings.

Q2. In what way are reserved words better than keywords?

As a language design choice, reserved words are better than keywords because the ability to redefine keywords can be confusing.

Q3. Which category of C++ reference variables is always aliases?

Category of C++ reference variables is always aliases when a C++ pointer is set to point at a named variable, the pointer, when dereferenced.

Q4. Define binding and binding time.

Binding is an association between an attribute and an entity, such as between a variable and its type or value, or between a variable and its type or value, or between an operation and a symbol. Binding time is the time at which a binding takes place.

Q5. After language design and implementation, what are the four times bindings can take place in a program?

  1. Static
  2. Stack-dynamic
  3. Explicit heap-dynamic
  4. Implicit heap-dynamic variables.

In some books authors refer following 4 types of time binding after design and implementation.

  • Language design time —  bind operator symbols to operations
  • Language implementation time– bind floating point type to a representation
  • Compile time — bind a variable to a type in C or Java
  • Load time — bind a C or C++ static variable to a memory cell)
  • Runtime — bind a non-static local variable to a memory cell

 

Q6. Define static binding and dynamic binding.

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.

Q 7. What are the advantages and disadvantages of dynamic type binding?

Advantages: Dynamic type binding provides flexibility and no definite types declared (PHP, JavaScript).
Disadvantages: Dynamic type binding add to the cost of implementation and type error detection by the compiler is difficult.

Q 8. How is a reference to a nonlocal variable in a static-scoped program connected to its definition?

A reference to a non-locally variable in a static-scoped language with nested subprograms requires a two-step access process:

  1. Find the correct activation record instance
  2. Determine the correct offset within that activation record instance

Q 9. Describe a situation when a history-sensitive variable in a subprogram is useful.

History-sensitive variables may be useful in subprograms data manipulation, in which some of the operations performed on variables, and function out, then the function is called again. In this way, the function does not have to take a variable as a parameter, but only to return it.

                Programming Problems

Q10. 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?

  1. sub1
  2. sub2

iii. sub3

  1. b) Repeat part a, but assume dynamic scoping.

Answer:

(a)

  1. Sub1
  2. Sub1
  3. main

(b)

  1. sub1
  2. sub1
  3. sub1

Questions from Book Chapter 5

Q7: 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 + ”
“); } function sub2() { var x; x = 10; sub1(); } x = 5; sub2();

Answer: Static scoping: x is 5                                 Dynamic Scoping: x is 10

Q8: 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.

Answer:

In sub1:

Variable Where Declared
a Sub1
x Sub1
y Sub1
z Main

In sub2:

Variable Where Declared
A Sub2
B Sub2
X Sub2
Y Sub1
Z Main

In sub3:

Variable Where Declared
A Sub3
X Sub3
W Sub3
Y Main
Z Main

 

Q9: 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.

Answer: Assuming static scoping rules are used in the given python program:

Variable               Where Declared

In sub1:

a =7        sub1()

y =9        sub1()

z =11      sub1()

x =1        main()

In sub2:

a =13     sub2()

x =15     sub2()

w =17    sub2()

y =3        main()

z =5        main()

In sub3:

a =19     sub3()

b =21     sub3()

z =23      sub3()

w =17    sub2()

x =15     sub2()

y =3        main()

Q10: 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.

Answer:

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 1
C 1

 

Q11: Consider the following skeletal C program: void fun1(void); /* prototype */ void fun2(void); /* prototype */ void fun3(void); /* prototype */ void main() { int a, b, c; . . . } void fun1(void) { int b, c, d; . . . } void fun2(void) { int c, d, e; . . . } void fun3(void) { int d, e, f; . . . } Given the following calling sequences and assuming that dynamic scoping is used, what variables are visible during execution of the last function called? Include with each visible variable the name of the function in which it was defined. a. main calls fun1; fun1 calls fun2; fun2 calls fun3. b. main calls fun1; fun1 calls fun3. c. main calls fun2; fun2 calls fun3; fun3 calls fun1. d. main calls fun3; fun3 calls fun1. e. main calls fun1; fun1 calls fun3; fun3 calls fun2. f. main calls fun3; fun3 calls fun2; fun2 calls fun1.

Answer:

Variable Where Declared
D, e, f Fun3
C Fun2
B Fun1
A Main
D,e,f Fun3
B,c Fun1
A Main
B,c,d Fun1
E,f Fun3
A Main
B,c,d Fun1
E,f Fun3
A Main
C,d,e Fun2
F Fun3
B Fun1
A Main
B,c,d Fun1
E Fun2
F Fun3
A Main

 

Leave a Reply