Computer Science 3 - 2014 - Test 1 - Parva and some other tools

This should take no longer than 45 minutes and is simply intended to probe whether you have understood the material in the prac questions and the early part of the course. Answer on the question sheet (4 sides).

"The purpose of computing is insight, not numbers" (Richard Hamming)

1. You should get this right! Mine is Pat Terry, 63T0844. What is yours? [1]

2. What is the significance of each of the numbers 16384, 32767, 16411, and why? (Be careful!) [4]

3. Here is a simple Parva program (assume it is stored in the file Silly.pav):

    void main() {
    // A rather simple Parva program - but what does it calculate?
      int a, b;
      read("Supply a and b (0 stops) ", a, b);  /*  first pair of data */
      while (a != 0) {
        int c = a - a / b * b;
        write(a, b, c);
        read(a, b);                             // read the next data pair
      }
    } // main

(a) What does it attempt to calculate and print? [2]

(b) It would not execute sensibly if it were given the data pair { 8 , 0 } for fairly obvious reasons. Would you classify such a fault as an example of an syntactic error, a static semantic error. or a dynamic semantic error - and why? [2]

If you were to translate this program to Java using the Parva2ToJava program, it would produce the following code (Silly.java):

    import library.*;

    class Silly {

      static public void main(String[] args) {
        int a, b;
        { IO.write("Supply a and b (0 stops) "); a = IO.readInt(); b = IO.readInt(); }
        while (a != 0) {
          int c = a - a / b * b;
          { IO.write(a); IO.write(b); IO.write(c); }
          { a = IO.readInt(); b = IO.readInt(); }
        }
      } // main

    } // Silly

This seems to have lost something (the comments) and gained something (extra, apparently unnecessary, braces { } ) - it might be preferable to have generated code like this:

    import library.*;

    class Silly {

      static public void main(String[] args) {
      // A rather simple Parva program - but what does it calculate?
        int a, b;
        IO.write("Supply a and b (0 stops) ");
        a = IO.readInt(); b = IO.readInt();     /*  first pair of data */
        while (a != 0) {
          int c = a - a / b * b;
          IO.write(a); IO.write(b); IO.write(c);
          a = IO.readInt(); b = IO.readInt();   // read the next data pair
        }
      } // main

    } // Silly

(c) Is the loss of the comments critical? Why, or why not? [2]

(d) Might the extra { braces }, in fact, be necessary in such translations? Explain! [4]

(e) If you executed the sequence of commands

(a) Parva2ToJava Silly.pav -l
(b) Parva Silly.pav -l
(c) Javac Silly.java
(d) Java Silly

at which stage(s) would an error like that suggested earlier in (b) become apparent, and in what way would it be reported? [4]

4. Question 3(e) mentions four systems programs. Classify each one as being either (a) a native code compiler (b) an interpretive compiler (c) an interpreter (d) a high-level compiler (e) an assembler or (f) a decompiler. [4]

(a) Parva2Java

(b) Parva

(c) Javac

(d) Java

5. The Parva2Java translator was developed in C# and translates Parva programs to their Java equivalents. Presumably - since C# and Java are so similar - one could now use it as a model and develop a Parva2ToCSharp (P2C) translator using Java to translate a Parva program to a C# equivalent quite quickly. Complete the following T Diagrams to show how this might be done and how P2C could be used: [8]

                     .--------------------------.          .--------------------------.
                     |          P2C.java        |          |          P2C             |
                     |  Parva ---------->   C#  |          | Parva  ---------->       |
                     |                          |          |                          |
                     `-------.          .--------------------------.          .-------'
                             |          |          Javac           |          |
                             |   Java   |  Java  -------->         |          |
                             |          |                          |          |
                             `------------------.          .------------------'
                                                |          |
                                                |          |
                                                |          |
                                                |----------|
                                                |          |
                                                |          |
                                                |          |
                                                |----------|
                                                |          |
                                                |  Intel   |
                                                |          |
                                                `----------'


  .--------------------------.          .--------------------------.          .--------------------------.
  |         Sieve.pav        |          |        Sieve.cs          |          |         Sieve.exe        |
  |   N    ----------> Primes|          |   N   -----------> Primes|          |   N     ---------> Primes|
  |                          |          |                          |          |                          |
  `-------.          .--------------------------.          .--------------------------.          .-------'
          |          |           P2C            |          |                          |          |
          |   Parva  | Parva  -------->    C#   |    C#    |        -------->         |  Intel   |
          |          |                          |          |                          |          |
          `------------------.          .--------------------------.          .-------+----------|
                             |          |                          |          |       |          |
                             |          |                          |          |       |  Intel   |
                             |          |                          |          |       |          |
                             |----------|                          |----------|       `----------'
                             |   JVM    |                          |          |
                             | Interpr  |                          |  Intel   |
                             |  Intel   |                          |          |
                             |----------|                          `----------'
                             |          |
                             |  Intel   |
                             |          |
                             `----------'

6. Here's another simple Parva program - the sort you might find a first year student writing:

    bool b(int n) {
      return n == i(n);
    } // b

    int i(int n) {
      int r = 0;
      while (n != 0) {
        r = 10 * r + n % 10;
        n = n / 10;
      }
      return r;
    } // i

    void main() {
      int n;
      read("Supply n (0 stops) ", n);
      while (n != 0) {
        write(b(n));
        read(n);
      }
    } // main

Simplicity is sometimes deceptive!

(a) What do you suppose the program is meant to achieve? [2]

(b) Suggest better names for the identifiers used for the functions/methods. [2]

(c) Why would this code not compile with the Parva compiler you have been using? [2]

(d) How could you correct it so that it would compile and execute satisfactorily? No, you don't have to rewrite it completely. The answer is very simple! [3]

(e) What do you suppose is the Big Idea I am trying to get across to you in this question? [1]


Home  © P.D. Terry