






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Your full name:
Your UCSD ID number:
This exam is closed book and closed notes
Total number of points in this exam: 231 + 25 extra credit
This exam counts for 25% of your overall grade in the class
Problem Number of Points Problem 1 15 Problem 2 30 Problem 3 45 Problem 4 16 Problem 5 35 + 25 extra Problem 6 50 Problem 7 40 Total 231 + 25 extra
Problem 1 (History) (15 points)
Circle the correct answer for each of the questions below:
a. Which language was the first to try to bring together programming for scientific application and for business applications?
b. Why was separate compilation an important feature of early FORTRAN?
c. What can be said of Smalltalk?
d. What language introduced the concept of a “block structure”?
e. What design criterion was used extensively in ALGOL 68?
Problem 3 (Semantics)
a. (5 points) What is the use of static semantic? What does one use to define static se- mantic? Used to describe the legal form of programs in ways that a BNF grammar cannot. At- tributed grammar.
b. (15 points) What are the three kinds of ”dynamic semantics” that we have seen in class. Name one good use of each.
c. (5 points) Compute the weakest precondition for the following statement and postcondi- tion (assume that all variables are integers):
z = 2 * (c - 3) - 1 { z > 1 }
{c > 4} by the assignment axiom
c. (20 points) Compute the weakest precondition for the following statement and postcon- dition (assume that all variables are integers):
if (a != 3) a = 2*a-1; else a -= 2; { a > 0 }
By the assignment axiom: {a > 0} a = 2*a - 1 (S1) { a > 0} and: {a > 2} a -=2 (S2) { a > 0}
We need to find the weakest P such that:
{P and (a! = 3)}S 1 {a > 0 }, {P and (a == 3)}S 2 {a > 0 } {P } if (a! = 3) then S1 else S 2 {a > 0 }
P = {a > 0 } is the weakest precondition because:
Problem 5 (Types)
a. (10 points) Give two examples of languages that uses static type binding, and two exam- ples of languages that uses dynamic type binding.
Static type binding: C, Java
Dynamic type binding: Lisp, Perl, Javascript
b. (15 points) Give an example of a language construct in a statically typed language that requires dynamic type checking. Give an example of code that causes a type error unde- tectable by a compiler
The C union
union { int i; char *s; } x;
x.i = 1; ... printf(x.s);
c. (10 points) What are the trade-offs between name type compatibility and structure type compatibility? which type compatibility scheme do most language use?
Name compatibility is easy to implement but very restrictive, while structure compat- ibility is difficult to implement but more flexible. Most languages use a combination of both (e.g., declaration compatibility).
d. [EXTRA CREDIT] Multidimensional arrays can be stored in row major order, as in C, or in column major order, as in Fortran.
(i) (5 points) Why it is important for a programmer to know which way arrays are stored by the language (Circle the correct answer):
b. (25 points) Write a recursive LISP function called inserta that takes a list as input and returns a new list in which symbol a has been inserted after every element of the original list. If the input list is empty, then inserta returns the empty list. Do NOT use setq, let, defvar, or any other way to declare variables. Here is an example:
CL-USER(1): (inserta ’(a b c d)) (a a b a c a d a)
(defun inserta (l) (cond ((null l) nil) (t (cons (car l) (cons ’a (inserta (cdr l)))))))
c. (10 points) What does the following Common Lisp form evaluate to?
(mapcar #’(lambda (x) (list x (list x) )) ’(a b c))
((a (a)) (b (b)) (c (c)))
Problem 7 (Expressions) Consider the following program in C syntax:
int fun (int *i) { *i += 5; return 2; }
void main () { int x = 3; if ((x == 2) && (fun (&x))) x++; x = x + fun (&x); printf("%d\n",x); }
What is the output of this program assuming:
5
10
15
15