\magnification=1200 \nopagenumbers \headline= {Spring 1985\hfil Comp 190: {\it Programming Languages}\hfil190-11.\folio} \bigskip \centerline{Final Exam} \smallskip \centerline{9:00 {\sevenrm AM} -- 12:00 Noon, May 1, 1985} \bigskip\noindent Problem 1. (18 points) \par For each of the following six programming languages, Ada, BASIC, COBOL, FORTRAN, LOGO, and Pascal, characterize those application areas which are especially well-suited for that language. Mention specific features of the language to justify your claims. (About three sentences per language, please.) \medskip\noindent Problem 2. (12 points) \par Consider the following Pascal program: \medskip \begingroup \obeylines\obeyspaces\tt program M ; \ \ \ var A: array[1..2] of integer ; \ \ \ procedure P(Y, Z: integer) ; \ \ \ \ \ \ var X: integer ; \ \ \ \ \ \ procedure Q(var X: integer) ; \ \ \ \ \ \ \ \ \ begin (*{\it body of Q}\ *) \ \ \ \ \ \ \ \ \ \ \ \ X := X - A[1] ; \ \ \ \ \ \ \ \ \ \ \ \ P(X, X) \ \ \ \ \ \ \ \ \ end (*{\it body of Q}\ *); \ \ \ \ \ \ begin (*{\it body of P}\ *) \ \ \ \ \ \ \ \ \ Z := Y div Z \ \ \ \ \ \ \ \ \ Q(A[A[2]]) ; \ \ \ \ \ \ end (*{\it body of P}\ *); \ \ \ begin (*{\it body of M}\ *) \ \ \ \ \ \ A[1] := 2 ; \ \ \ \ \ \ A[2] := 1 ; \ \ \ \ \ \ P(A[1], A[2]) \ \ \ end (*{\it body of M}\ *) . \endgroup \medskip\noindent If you execute this program, it should (in four or fewer procedure calls) ``bomb'' when it tries to divide an integer by zero. Draw the stack and display at the moment when the program bombs. Be sure to show on the stack all variables, parameters, and dynamic and static environment chains. \medskip\noindent Problem 3. (10 points) \par Assume you have a programming language with four operators, %emacs-mathOK $\clubsuit$, $\diamondsuit$, $\heartsuit$, and $\spadesuit$, with left-to-right associativity and the following operator precedence rules: \smallskip\noindent {\settabs\+\indent&Highest precedence\qquad&\cr \+&Highest precedence&$\clubsuit$\cr \+&Middle precedence&$\diamondsuit\quad\heartsuit$\cr \+&Lowest precedence&$\spadesuit$\cr} \smallskip\noindent Show both the tree form (Fig. 6-2, page 153) and postfix notation for the expression: $$(a\ \diamondsuit\ b\ \spadesuit\ c) \ \heartsuit\ d\ \clubsuit\ e\ \diamondsuit\ (f \clubsuit g)$$ \vfil\eject\noindent Problem 4. (10 points) \par An Ada package has two parts, for example: \medskip \begingroup \obeylines\obeyspaces\tt package INT\_STACK\_TYPE is \quad\dots end INT\_STACK\_TYPE package body INT\_STACK\_TYPE is \quad\dots end INT\_STACK\_TYPE \endgroup\medskip\noindent What is the purpose of each of these two parts? Which part(s) must be looked at by (a) someone using the package, and (b) someone modifying the package? \medskip\noindent Problem 5. (7 points) \par Draw the state of a LOGO interpreter using a reference count garbage collector after it executes each of the following four statements. \medskip \begingroup \obeylines\obeyspaces\tt MAKE "L1 [5 6] MAKE "L2 (BUTFIRST :L1) MAKE "L1 (FPUT 4 :L2) MAKE "L2 (BUTFIRST :L1) \endgroup \medskip\noindent Problem 6. (6 points) \par Consider the following program written in a generic language: \medskip \begingroup \obeylines\obeyspaces\tt program foo ; \ \ \ var n: integer ; \ \ \ function g(): integer; \ \ \ \ \ \ begin \ \ \ \ \ \ \ \ \ return( n+1 ) \ \ \ \ \ \ end ; \ \ \ function f(): integer; \ \ \ \ \ \ var n: integer ; \ \ \ \ \ \ begin \ \ \ \ \ \ \ \ \ n := 1 ; \ \ \ \ \ \ \ \ \ return( g() ) \ \ \ \ \ \ end ; \ \ \ begin \ \ \ \ \ \ n := 5 ; \ \ \ \ \ \ print( f() ) \ \ \ end ; \endgroup \medskip\noindent What is the result of executing this program using both (a) dynamic and (b) static scope rules? Explain your answer. \medskip\noindent Problem 7. (6 points) \par Give at least two good reasons why you can't write a general-purpose quicksort routine in Pascal. \vfil\eject\noindent Problem 8. (6 points) \par Assume that {\tt x}, {\tt y}, and {\tt z} have been declared to be integer variables in Ada and that {\tt x} and {\tt y} are both positive. Write two or three lines of Ada that will set the integer variable {\tt z} to the minimum of 1000000000 and {\tt x+y}. Use Ada's exception-handling mechanism to make sure that your code works correctly even if the computation of {\tt x+y} signals the Ada exception {\tt OVERFLOW}. \medskip\noindent Problem 9. (5 points) \par Make a movie showing the stack of temporary values of a compiled Pascal program executing the expression {\tt x+(y*z)-(x mod z)} when {\tt x} is 5, {\tt y} is 6, and {\tt is 7}. \medskip\noindent Problem 10. (5 points) \par Write a Pascal procedure {\tt increment2} which takes two integer parameters. The effect of {\tt increment2} is to make each actual parameter one greater than it was before the call. For example: \medskip \begingroup \obeylines\obeyspaces\tt increment2(A[i], A[j]) ; \endgroup\medskip\noindent is equivalent to: \medskip \begingroup \obeylines\obeyspaces\tt A[i] := A[i] + 1 ; A[j] := A[j] + 1 ; \endgroup\medskip\noindent if {\tt i} and {\tt j} are different and is equivalent to: \medskip \begingroup \obeylines\obeyspaces\tt A[i] := A[i] + 1 ; \endgroup\medskip\noindent if {\tt i} and {\tt j} are the same. \medskip\noindent Problem 11. (5 points) \par Pascal prohibits programmers from directly using the value returned by a pointer-valued function as a pointer reference. For example, you cannot use {\tt f(5)\char'136} in a Pascal expression, even if {\tt f} is function that maps integers into pointers. Can you think of any justification for this restriction? Explain your reasoning. Do you anticipate any problem in implementing an extension of Pascal which does not have this restriction? \medskip\noindent Problem 12. (5 points) \par It is generally thought that FORTRAN programs are more efficient than Pascal programs; however, it is quite likely that, if you take a Pascal program and translate it into FORTRAN in a straightforward manner (assume the Pascal program had no recursion), the FORTRAN program will use more storage. Discuss briefly (one paragraph) why this is so. \medskip\noindent Problem 13. (5 points) \par Write FORTRAN {\tt READ} and {\tt FORMAT} statements to input 80 one-digit numbers, typed one per column on a card, into an integer array {\tt C}. \medskip\noindent \bye