1. Describe the five program structure/flow
    abstractions mentioned in the text.

    _SEQUENCE__ _SELECTION_ _REPETITION COMPOSITION PARALLELISM
  2. Fill in the blank:
    1. "Computers are TOOLS, and computing scientists are TOOLSMITHS."
    2. A procedure that returns a value is called a FUNCTION procedure.
    3. Before using the value of a variable for the first time in a program, we need to DECLARE and INITIALIZE it.
    4. TRUE: The word FUNCTION is a legal identifier.
    5. The three loop structures in Modula-2 are WHILE/DO/END, REPEAT/DO/UNTIL, FOR/DO/END.
    6. A new type made from a sequence of consecutive values of an existing type is called a SUBTYPE of the host type.
    7. What English part of speech should a variable generally be? NOUN
    8. The ABS function is used to determine how far a real/integer is from zero.
    9. The function that evaluates natural logarithms on real numbers is found in the REALMATH standard library.
    10. Redirection of the standard input/output channels is handled by the REDIRSTDIO library.
    11. A procedure that invokes itself is called a RECURSIVE procedure.
  3. Explain all the differences between the following, and give examples of each:
    1. reserved words: Syntactical punctuation, part of the language. e.g., MODULE, BEGIN, VAR
    2. standard identifiers: Names builtin to the language, e.g. names of builtin types. e.g. CHAR, CARDINAL, REAL
    3. standard library items: Names of procedures/types/variables included in the standard library modules that come with every ISO Modula-2 installation. e.g., WriteString, ReadReal.
  4. Write a complete declaration for each of the following data types:
    1. A string type: TYPE String = ARRAY [0..10] OF CHAR;
    2. An enumeration type representing the months of the year: TYPE Month = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
    3. A type holding one string for each month of the year: TYPE MonthString = ARRAY Month OF String;
    4. A type for only the uppercase letters: TYPE UpperCase = ['A'..'Z'];
    5. A type for an m by n matrix of booleans: TYPE BoolMatrix = ARRAY [1..m] OF ARRAY [1..n] OF BOOLEAN
  5. Evaluate each of the following expressions, or if it produces an error, describe the error. Assume all necessary IMPORTs have been done. Assume the following declaration and initialization:
    	myApple : ARRAY [0..5] OF CHAR;
    	myApple := "granny smith";		
    1. 11 REM 0 : ERROR (DIVIDE BY ZERO)
    2. 12.2 MOD 5 : ERROR (MOD NOT DEFINED ON REALS)
    3. WriteFixed (23.468, 4, 8); : " 23.4680" (one space before)
    4. ~(3<5)&~3#5: ERROR (NOT 3) (NOT IS NOT DEFINED ON CARDINALS)
    5. LENGTH(myApple) : 6
    6. (LENGTH(myApple) > 0) AND (CAP(myApple[0]) = 'G') : TRUE
    7. Strings.Compare (myApple, "Granny Smith") : GREATER
    8. 'C' > 'A' : TRUE
    9. 'C' - 'A' : ERROR (MINUS NOT DEFINED ON CHAR)
    10. ORD ('C') - ORD ('A') : 2
    11. 'C' + 'A' : "CA" (string)
    12. ORD (March) (using the enumeration type you defined above): 2
  6. Show the results (output and/or errors) both with and without the VAR in the formal parameter list and explain.
    MODULE SwapTest;
    FROM SWholeIO IMPORT
    	WriteCard;
    
    PROCEDURE Swap (VAR a, b: CARDINAL);
    VAR
    	tmp : CARDINAL;
    BEGIN
    	tmp := a;
    	a := b;
    	b := tmp;
    END Swap;
    
    VAR
    	a, b: CARDINAL;
    BEGIN
    	a := 2;
    	b := 5;
    	Swap (a, b);
    	WriteCard (a, 0);
    	WriteCard (b, 0);
    END SwapTest.
    
    With the VAR, Swap works as expected: " 5 2" is the output.
    Without the VAR, the values of a and b in the global context are unchanged; the output is " 2 5".
  7. Consider the following procedure:
    PROCEDURE SumPlus1 (VAR a, b: CARDINAL): CARDINAL;
    	(* returns a+b+1 *)
    BEGIN
    	INC (a);
    	RETURN a + b;
    END SumPlus1;
    
    Assume there is a global variable x := 5. If we invoke SumPlus1 (x, x), we might expect the result to be 11, but it is not. What is returned instead, and why? How would you fix this?

    The result of SumPlus1(x,x) is 12, because both formal parameters a and b are aliases for the actual parameter x. So after the INC(a), both a and b have value 6. If we want to preserve the semantics of SumPlus1 returning a+b+1 without modifying the actual parameter, we could just take out the VAR in the formal parameter list. Alternately, we could just make b a value parameter and leave a as a variable parameter; that way, the actual parameter linked to a still gets incremented. Either solution is acceptable.