Docsity

Midterm Exam with Key for Programming Languages | CSE 130, Exams of Computer Science

Partial preview of the text

Download Midterm Exam with Key for Programming Languages | CSE 130 and more Exams Computer Science in PDF only on Docsity!

MIDTERM EXAMINATION - CS130 - Spring 2005

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?

  1. PL/I X
  2. ALGOL 60
  3. Ada

b. Why was separate compilation an important feature of early FORTRAN?

  1. it made it possible to detect syntax errors more precisely
  2. it made it possible to tolerate frequent machine downtimes X
  3. it allowed for better code reuse

c. What can be said of Smalltalk?

  1. it is faster than C++
  2. it is less object-oriented than Java
  3. it is more object-oriented than C++ X

d. What language introduced the concept of a “block structure”?

  1. Ada
  2. ALGOL 60 X
  3. SIMULA 67

e. What design criterion was used extensively in ALGOL 68?

  1. Readibility
  2. Safety
  3. Orthogonality X

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.

  • Axiomatic semantics. Good for proving program correctness.
  • Denotational semantics. Good basis for an interpreter.
  • Operational semantics. Good for describing the semantics to a programmer.

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:

  • {P and (a! = 3)} =⇒ {P }, which is the weakest precondition for S 1 {a > 0 }.
  • {P and (a == 3)} = {a == 3}, which is a valid precondition for S 2 {a > 0 }.

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):

  1. To limit the overhead of loop index computation
  2. To increase data locality X
  3. To save storage space (ii) (20 points) In a way similar to what we’ve done in class, write the access function for 3-dimensional column-major arrays. Assume an array a, with dimension sizes m, n, and p, with address of the first element a[1,1,1], and with element size element size. location(a[i,j,k]) = address of a[1,1,1] + ((k-1)mn + (j-1)*m + i-1) * element size

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:

  1. (10 points) Left-to-right operand evaluation (for ALL operators), short-circuited boolean expressions

5

  1. (10 points) Left-to-right operand evaluation (for ALL operators), non-short-circuited boolean expressions

10

  1. (10 points) Right-to-left operand evaluation (for ALL operators), short-circuited boolean expressions

15

  1. (10 points) Right-to-left operand evaluation (for ALL operators), non-short-circuited boolean expressions

15