Copyright © 2006 Gradiance Corporation.

 

 

     

Programming Language Concepts

 

 



 

Questions on block structure, static and dynamic binding, and parameter passing.

 


1.  

Here is a sketch of a program with nested block structure:

{ int w, x, y; /* Block B1 */
    { int x, z; /* Block B2 */
        { int w, z; /* Block B3 */ }
    }
    { int x, y; /* Block B4 */
        { int w, y; /* Block B5 */ }
    }
}

There are a total of 11 different declarations for names using four different identifiers, w, x, y, and z. Assuming static scoping, as is usual in C and similar languages, find the scope for each of these declarations. Then, identify the correct scope from the list below.

 

 

 

 a) 

B1's w has scope B1-B3-B5

 

 b) 

B2's x has scope B2-B3

 

 c) 

B1's x has scope B1-B2

 

 d) 

B1's w has scope B1-B3

 

 

 

 

 

2.  

In this question, you may assume that g, h and i are global variables of a C program, while x is local to a particular function of that program. Classify the following four statements:

  1. int h
  2. int x
  3. i = 10
  4. #define g 100

According to whether they bind (a) names to values, or (b) names to locations. Also, classify them as to whether they (i) can be executed statically, or (ii) must be executed dynamically. Then, identify the correct statement from the list below.

 

 

 

 a) 

i = 10 binds a name to a value, dynamically.

 

 b) 

int x binds a name to a value, dynamically.

 

 c) 

#define g 100 binds a name to a value, dynamically.

 

 d) 

int h binds a name to a location, dynamically.

 

 

 

 

 

3.  

Here is a sketch of a program with nested block structure:

{ int w, x, y; /* Block B1 */
    { int x, z; /* Block B2 */
        { int w, y; /* Block B3 */ }
    }
    { int w, x; /* Block B4 */
        { int y, z; /* Block B5 */ }
    }
}

There are a total of 11 different declarations for names using four different identifiers, w, x, y, and z. Assuming static scoping, as is usual in C and similar languages, find the scope for each of these declarations. Then, identify the correct scope from the list below.

 

 

 

 a) 

B1's x has scope B1-B2

 

 b) 

B2's z has scope B2

 

 c) 

B2's z has scope B2-B3

 

 d) 

B5's z has scope B2∩B5

 

 

 

 

 

4.  

Here is some block-structured C code.

int w, x, y, z;
int i = 1;
int j = 2;
    {
    int i = 3;
    j = 4;
    w = i + j;
    }
x = i + j;
    {
    int j = 5;
    y = i + j;
    }
z = i + j;

Determine the values of w, x, y, and z, and then identify a correct value from the list below.

 

 

 

 a) 

z = 8

 

 b) 

x = 3

 

 c) 

y = 6

 

 d) 

y = 8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5.  

Here is a C function f:

int f(int x, *py, **ppz) {
    **ppz += 4;
    *py   += 3;
    x     += 2;
    return x+(*py)+(**ppz);
}

Suppose there are variables a, b, and c external to f as follows: a is a pointer to b; b is a pointer to c, and c is an integer with current value 1. If we call f(c,b,a), what is returned by f?

 

 

 

 a) 

12

 

 b) 

30

 

 c) 

19

 

 d) 

8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6.  

In a language that uses call-by-reference, a function f(x,y) is defined as follows:

x = x + 3;
y = y + 4;
return x+y

If a variable a is assigned the value 5, and then f(a,a) is called, what value does f return?

 

 

 

 a) 

28

 

 b) 

17

 

 c) 

31

 

 d) 

24