Midterm 2, CSCI 3155
October 28th, 2004

You have 70 minutes to answer these questions.  Please read all questions before you start.  This is a closed-book exam.  The points for a question are split evenly between its parts and each question is worth 20 points.  Please remember that you are bound by the CU honor code.

  1. [Topic: Generic subprograms, skill 14.1]
    Write a generic subprogram in C++ or Java syntax that takes two arguments of the same type and swaps their values.   Your subprogram should work on arguments of any type as long as both the arguments are of the same type.  Assume a language that uses in-out parameter passing mode (e.g., pass-by-reference) and ignore inclusion polymorphism for this question.
  2. [Topic: Parameter passing modes, skill 12.2]
    Write a MYSTERY program that will print out different values with pass-by-name and pass-by-reference.  Explain why your program behaves in this fashion.
  3. [Topic: Synthesis]
    Consider two procedure types:
    TYPE P1 = PROCEDURE (a: P1A): P1R;
    TYPE P2 = PROCEDURE (a: P2A): P2R;
    Assuming that the language uses pass-by-reference.
    1. What relationship should hold between P1A and P2A in order for P1 <: P2 to hold.  Assume P1R = P2R for this part.  Explain your answer.
    2. What relationship should hold between P1R and P2R in order for P1 <: P2 to hold.  Assume P1A = P2A for this part.  Explain your answer.
  4. [Topic: Pointers and memory management, skills 8.3, 8.4]
    Give one advantage of reference counting over mark-and-sweep and one advantage of mark-and-sweep over reference counting.  Clearly defend your answer.
  5. [Topic: Data types, skill 7.3]
    Give an example program fragment (i.e., give code) that declares and uses a union type.  If you don't use C++/C union syntax be sure to explain your code.  Argue why record types would be less appropriate than the union type for your example.