CpSc 376 Exam 2
Fall 2014
Name: ______
1. (10 pts). Garbage collection can be performed in either a lazy or eager approach. Which is better and why?
2. (10 pts) What major difficulty does a compiler writer have in the implementation of variant records? How might this be solved?
3. (10 pts) If a programming language permits sub programs to be defined within sub programs, what is the limit on the depth of the nesting? Defend your answer.
4. (5 pts). Assume a two dimensional array with 5 columns and 3 rows is declared and stored contiguously in memory. If the array was stored in column major order would row 2 column 3 be the 1st, 2nd, 3rd, … 15th element stored? If it were stored in row major?
Row Major: ______
Column Major: _____
5. (10 pts) Provide the results of executing the following Lisp commands. Assume that list1 contains (a b c d) and that list2 contains (x y z).
a. (car (car list1)) ______
b. (cdr (append list1 list2)) ______
c. (reverse list1) ______
d. (replaca list2 ‘G) ______
e. (cdr (cdr (cdr list2) ) ) ______
6. (5 pts) Some languages (not C++) that use short circuit evaluation for evaluating conditionals provide special Boolean operators for and and or to expressly permit short circuit evaluation. Describe or name the two operators and explain the operation (i.e., semantics) of both of these operators.
7. (5 pts) What is the value of sum1 and sum2 if binary expressions are evaluated right to left? Operator precedence is still in effect.
int fun (int *k) { sum1: sum2:
*k = *k + 4;
return 3 * (*k) -1;
}
void main() {
int i = 10, j = 10, sum1, sum2;
sum1 = ( i/2 ) + fun ( &i );
sum2 = fun( &j ) + ( j/2 );
}
8. (5 pts) Dijkstra defined concurrent guarded conditionals as shown in the code below. What is the purpose of the following code? What happens if x and y are equal?
if x>=y -> max := x
[ ] y>=x -> max := y
fi
9. (5 pts) In what ways are co-routines different than conventional subprograms?
10. (10 pts) Describe and compare the two methods for preventing dangling pointers.
11. (10 pts) Name and describe the three semantic models of parameter passing.
12. (10 pts) Consider the code below:
//main program
var A, B, C : integer;
procedure SUB1();
var A, Y, Z : integer;
begin
. . .// **Here
end; // of SUB1
procedure SUB2;
var A, X, W
begin
procedure SUB3 ;
var A, B, Z : integer;
begin
. . . // ** Here
end; // of SUB3
. . . // ** Here
end; //of SUB2
begin //of MAIN
. . .
end; // of MAIN
Given the following calling sequence and assuming dynamic scoping is used, list all the variables (along with the program unit where they are declared) that are visible during the execution of the last subprogram activated.
a. main calls sub1, sub1 calls sub2, sub2 calls sub3
b. main calls sub2, sub2 calls sub3, sub3 calls sub1
13. (10 pts) Consider the following code written in C syntax:
void main() {
int value = 2, list[5] = {1, 3, 5, 7, 9 };
swap (value, list[0]);
swap (list[0], list[1]);
swap (value, list[value]);
}
void swap (int a, int b) {
int temp = a;
a = b;
b = temp;
}
For each of the following parameter-passing methods, what are the values of the variables value and list after each call to swap?
a. Pass-by-value
value / list[0] / list[1] / list[2] / list[3] / list[4]1st swap
2nd swap
3rd swap
b. Pass by value-result
value / list[0] / list[1] / list[2] / list[3] / list[4]1st swap
2nd swap
3rd swap
c. Pass by name
value / list[0] / list[1] / list[2] / list[3] / list[4]1st swap
2nd swap
3rd swap