Starting from:

$30

Programming Languages Concepts Homework 2

Programming Languages Concepts

Homework 2
Submission: Please submit your homework via Canvas. It’s okay if you
submit a scanned version of your on-paper answers, but please make sure
your scanned version is legible.
1. (8 points) Consider the C program given below. You will be asked
to determine which variables are visible in a number of different situations. In each case, identify each variable by its name and the line
number of its declaration.
1 int h, i;
2 void B(int w) {
3 int j, k;
4 i = 2*w;
5 w = w+1;
6 ...
7 }
8 void A (int x, int y) {
9 float i, j;
10 B(h);
11 i = 3;
12 ...
13 }
14 void main() {
15 int a, b;
16 h = 5; a = 3; b = 2;
17 A(a, b);
18 B(h);
19 ...
20 }
(a) C uses static scoping. Say which identifiers (including variables
and function names) are visible in the bodies of each of the functions: main, A, B.
1
(b) If C used dynamic scoping and the calling sequence is main calls
B. Say which identifiers would be visible in B.
(c) If C used dynamic scoping and the calling sequence is main calls
A. Say which identifiers would be visible in A.
(d) If C used dynamic scoping and the calling sequence is main calls
A; A calls B. Say which identifiers would be visible in B.
2. (3 points) Find some online material to learn C++’s namespace mechanism. Explain briefly how it works and its benefits.
3. (3 points) During the execution of a Java program, can a variable be
visible but not allocated? Can a variable be allocated but not visible?
Explain your answers.
2

More products