Programming Languages: Sample Final Exam

Problem 1

Consider the following pseudo-code in an imperative language:
int x = 1, n = 10;

function f () {
   int x = 3;
   function g (int c,d) {
      c = c-1;
    } /* end of g */

/* body of f */
} /* end of f */

function h () {
   int n = 2;

} /* end of h */

main()  { h(); }

Problem 2

A two-dimensional array may be implemented as a "true" two-dimensional array or as an array of pointers to one-dimensional arrays. Suppose that For each of the two implementations, explain what steps are involved at run-time in computing the address of the expression A[I,J]. (Ignore bounds checking).

Problem 3

Problem 4

Consider the following Ada code framework. There is one client and ten servers. The client computes a series of ten questions and asks one question to each server. The server first returns a quick answer (perhaps just an acknowledgement) and then thinks at greater length and returns a more thoughtful answer.

type query = ... end;
type answer = ... end;

task type server is
   entry request(q: in query; a: out answer);
   entry returnAnswer(a: out answer);

task body server is
qy : query;
an: answer;
begin accept request(q: in query, a: out answer) do
          qy = q;
          an := quickAnswer(q);
        end request
        an := thoughtfulAnswer();
        accept returnAnswer(out an: answer)
          end returnAnswer;
      end loop;
end server;

procedure client is
s: array (1 .. 10) of server;
m : message;
a : answer;
begin for i in 1 .. 10 loop
         m := computeMessage(i);
       end loop
      for i in 1 .. 10 loop
      end loop
end client;
Assume that the functions "quickAnswer", "thoughtfulAnswer", "computeMessage", and "returnAnswer" are defined elsewhere.

For each of the following pairs of activity P,Q state whether P must occur before Q, Q must occur before P, or P and Q can proceed in parallel:

Problem 5

If an object has been allocated on the heap but is no longer accessible then its space should be reclaimed as free space on the heap.

Problem 6:

In Java the type "int" and the type "Integer" are quite different.

Problem 7:

Consider the following fragment of Ada code:
type Distance is new Float;
subtype Small is Float range 0.0 .. 1.0;

procedure P is

F: Float;
E : Distance;
S : Small;
J: Integer;

begin F := 0.0;
      for I in 1 .. 10 loop
         J := I + 12;
         F := F + 0.5;
         S := F;
         E := S;
       end loop;    
end P;