CompSci Tripos Past Papers

The Cambridge Computer Labs have published all of their past papers since 1993 on the web at http://www.cl.cam.ac.uk/tripos/papers.html. They are published in both Post Script and DVI formats, but not HTML. As many of the computers which I use do not have facilities for reading either format, I intend to republish them in HTML format as a useful resource for myself and other students. I intend to stay as true to the original formatting as possible, and will eventually have all of the originals reproduced here. The few which I have at the moment were chosen based on the fact that I was set them for homework this week...

I realise that this could raise (ahem) "copyright issues", so if anyone at the University with any authority in this matter sees this, I would appreciate it if you would mail me with your views or suggestions.

Thanks.

Since this assignment I have found that I can get hold of these papers for a mere forty pence per paper, so it's not as pressing an issue any more. It is useful to have these papers online I think though, so I plan to carry on anyway at some point, in my (as a friend would say) copious free time.


Currently stored:


1995 Paper 1 Question 2

Programming in Modula-3

What is meant by the signature of a procedure in Modula-3?

Describe the different parts of a signature and explain how arguments may be supplied when a procedure is invoked.

[10 marks]

1995 Paper 1 question 10

Programming in Modula-3

The following Modula-3 program uses exceptions. What does it print on its standard output? Explain your answer.
    MODULE Main;
    IMPORT Fmt, IO;
    EXCEPTION Bad;
    VAR j: CARDINAL := 0;

    PROCEDURE P (i: CARDINAL): CARDINAL RAISES {Bad} =
      BEGIN
        IF i = 0 THEN RAISE Bad END;
        RETURN j + 100 DIV i;
      END P;

    PROCEDURE Q (i: CARDINAL): CARDINAL RAISES {Bad} =
      BEGIN
        TRY
          RETURN P (i);
        FINALLY
          j := j + i + 1;
        END;
      END Q;

    VAR k: CARDINAL := 0;
    BEGIN
      TRY
        k := Q (1); k := k + Q (0); (**)
      EXCEPT
        Bad => IO.Put ("Bad Raised\n");
      END;
      IO.Put ("k = " & Fmt.Int (k) & "\n" &
              "j = " & Fmt.Int (j) & "\n");
    END Main.
Rewrite procedures P, Q and the main program so that they perform the same calculations and have the same side-effects without using the exceptions.

If the line marked (**) is replaced by the line:

    k := Q (1) + Q (0);
what can you say about the output from the program?

[20 marks]

1994 Paper 1 question 10

Modula-3 allows the user to declare arrays with any sort of contents - for instance arrays of integers, reals, TEXT or structures, but the index type for an array is restricted and in particular cannot be of type TEXT.

It is sometimes useful to achieve an effect analogous to having an array that can be indexed using values of type TEXT. One way of doing this is to use a structure known as a hash table: given an index of type TEXT an integer is compared using a hash function and this is then used to index an array. The library procedure Text.Hash computes a suitable integer from a TEXT value. Two complications arise. First the integer computed by the hash function may lie outside the valid range of index values for the array. Secondly two different TEXT objects may give rise to the same hash value.

The problems can be resolved first by reducing the raw hash value modulo the size of the array and arranging that each array entry refers to the start of a linked list of (index, value) pairs. Retrieving a value from the table involves accessing the array to obtain the correct list of pairs and then scanning the list to find an index vale that is identical (use the library function Text.Equal) with the TEXT index being sought. The corresponding value can then be returned. Storing into the table will involve adding a new (index, value) pair to one of the lists.

Design appropriate data structures for such a table, and write procedures to store and retrieve values, using the following signatures:

PROCEDURE Put(VAR table: Table; key, value: TEXT)
                                 RAISES {DuplicateKey}

PROCEDURE Get(READONLY table: Table; key: TEXT): TEXT
                                 RAISES {MissingKey}

[20 marks]

1994 Paper 1 Question 11

To be done asap.

1993 Paper 1 Question 12

A list in Modula-3 can be represented as a sequence of links. Each link is a record containing one value in the list and a reference to the rest of the list following the link. The following TYPE declaration specifies the required data structure:
   TYPE
     List = REF Link;
     Link = RECORD value: CARDINAL; rest: List:= NIL END;
A test program which exploits lists of this kind includes:
   VAR
     start: List;
   BEGIN
     start:= NIL;
     Put (10,start);
     Put (100,start);
     Put (1000,start);
     Print (start);
     Print (Reverse1 (start));
     Print (Reverse2 (start));
The procedure call Put (1000,start) will add a link containing the value 1000 to the end of the list which already includes the values 10 and 100.

The procedure Print writes out the values in a list in order.

The procedure Reverse1 and Reverse2 reverse a list in two different ways, equivalent to the ML functions:

   fun Reverse1 [] = []
     | Reverse1 (value::rest) = Reverse1(rest) @ [value];

   fun Reverse2 list =
      let
          fun rev ([], result) = result
           |  rev(value::rest, result)  = rev(rest, value::result)
      in
          rev(list, [])
      end;
Write the Modula-3 procedures Put, Print, Reverse1 and Reverse2.

1992 Paper 1 Question 12

Describe the facilities in Modula-3 for handling objects and inheritance. Illustrate your answer by defining a type Number with methods add, subtract, multiply, divide and text. Derive sub-types Integer and Real which include appropriate data fields and default methods. The intention is that it should be possible to take any two variables x and y of type Number and invoke x.add(y) to produce a new number (which will be an Integer or a Real as appropriate) corresponding to the sum of the values associated with x and y. The text method simply returns a TEXT string corresponding to the value. The following example shows use of the type Number:

    VAR  r := Create("3.14159");
    VAR  r := Create("42");
    VAR  s := r.add(i);
    VAR  j := i.add(Create("37"));
The sub-type associated with the values of i, j will be Integer, whereas that associated with the values of r, s will be Real.

Sketch the procedure to create a Number of appropriate type from an argument text string.
[Recall the library routines Scan.Int and Scan.Real which may raise the exception Scan.BadFormat.]

Sketch also the procedures that implement the add and text methods for Integer and Real. If an Integer and a Real are to be added, the value associated with the Integer should automatically be converted to a Real.


This page was created by Tom Lynn. Comments etc. to: thl22@cam.ac.uk.

W3C Wilbur Checked!