Assignment 6, Order of evaluation and subtyping
Due 10/12/04 12:30 a.m.
(Must be done with your group)

  1. [synthesis] Languages differ greatly in how they handle arrays.  In this question we will focus on one issue with arrays: what happens when one array is assigned to another (assuming the two arrays are of compatible types).  Consider, for example, two variables declared as follows in MYSTERY syntax:

    VAR a: ARRAY[1 TO 5] OF INTEGER;
    VAR b: ARRAY[1 TO 5] OF INTEGER;
    BEGIN ... END;

    What happens when we have an assignment a := b?   If the assignment is "by reference" (which is how Java works), variables of array type are represented as pointers and the outcome of the assignment is that a ends up pointing to the same array that b points to (i.e., alias).  If the assignment is "by value" (which is how Modula-3 works), then the assignment copies the contents of array b to array a

    1. Use this link to figure out if MYSTERY uses array assignment by reference or by value.  Your answer to this question should describe which one MYSTERY uses and also provide the evidence that enabled you to come to your conclusion.  You submissions may execute up to a total of 6 PRINT statements.  Additional attempts will be charged at 5% of this question's points per executed PRINT.
    2. Discuss the relative advantages and disadvantages of array assignment by value and array assignment by reference.  Refer to the "Characteristics" (i.e., first column) in Table 1.1 of Sebesta in your arguments.
  2. [skill 11.1] Give and explain an example that shows that ARRAY I OF T <: ARRAY I OF U, if T <: U is an incorrect subtyping rule for updatable arrays.
  3. [skill 11.1] Should a boolean type be a subtype of an integer type?  Defend your case.
  4. [skill 10.1] In this question we will discusses the issues with short-circuit evaluation.
    1. Give an example that illustrates the usefulness of short circuit evaluation.  Explain your example.
    2. Discover and indicate whether or not mystery uses short-circuit evaluation of boolean operators (i.e., AND).  Use this link to submit programs.  You should provide evidence that supports your argument.  Your program should not execute PRINT more than three times.  You will lose 5% of the points for this question for each additional executed PRINT. 
  5. [synthesis] Even though Java's narrowing conversions for primitives may fail (e.g., cause an overflow), they never result in a run-time exception (i.e., the program continues as if nothing bad happened).  An alternative to this is to stop program execution and report a problem.  Discuss the relative merits of the two alternatives.  You may want to read the section on "narrowing primitive conversions" and "widening primitive conversions" in the Java language definition before answering this question.
  6. [skill 10.4] How does the guarded command differ from an IF statement in C++?  Describe one situation where you would prefer to use the guarded IF statement over C++'s if statement.  Describe one situation where you would prefer to use C++'s if statement over the guarded IF statement.  Provide arguments that clearly support your answer.
  7. [synthesis] Consider two procedure types in MYSTERY syntax
    TYPE P1 = PROCEDURE (A1 a):R1, and
    TYPE P2 = PROCEDURE (A2 a):R2 

    Consider three possibilities for P1 <: P2

    1. A1 = A2 and R1 = R2
    2. A2 <: A1 and R1 = R2
    3. A1 = A2 and R1 <: R2
    Answer the following questions:
    1. Discuss which of the above three possibilities you prefer.  Provide clear examples and arguments to support your preference.
    2. Assuming that MYSTERY allows subtypes to be assigned to supertypes, uses structural equality, and uses the obvious subtyping between subranges (e.g., [1 TO 10] <: [1 TO 100]) determine which of the above three possibilities is used by MYSTERY.  Use this link to submit programs.  You may submit up to six programs;  you will lose 5% of the points for each additional submission.  Provide evidence that supports your answer.
  8. [skill 10.4]Given one example when an if statement would be preferable to a switch statement and one example where a switch statement would be preferable to an if statement.  You may assume C/C++ semantics for if and switch statements.  Explain and justify your examples.
  9. [skill 10.3] Give an example that uses a pretest loop and justify why you needed a pretest loop (as opposed to a posttest loop).
  10. [skill 10.4] Give an example that uses a posttest loop and justify why you needed a posttest loop (as opposed to a pretest loop).
  11. [skill 10.2] Page 350, 3(c) (i.e., do not do 3(a) and 3(b))