total: 50 marks
  1. [4] 3FH: Decimal=63, octal=77B, binary=111111
  2. [4] 2Gb = 2^18 kilobytes = 262,144 kilobytes
  3. [12]
  4. Write a complete program module that does the following:
    1. [6] Declare a list of 30 students -- each student has a name string, a CARDINAL student ID, and a REAL GPA.
    2. [3] Initialize the list to default values using a single statement.
    3. [4] Write out the list to a file in binary format.
    4. [2] Display the size of the output file in LOCs.
    5. [4] Read back in from the file only the entry for the 17th student.
    MODULE ClassList;
    IMPORT StreamFile, RndFile, RawIO, STextIO, SWholeIO;
    FROM StreamFile IMPORT
    	ChanId, OpenResults, read, write, raw, old;
    TYPE
    	NameString = ARRAY [0..30] OF CHAR;
    	Student = RECORD
    		name : NameString;
    		ID : CARDINAL;
    		GPA : REAL;
    	END;
    	Class = ARRAY [0..29] OF Student;
    VAR
    	cmpt14x : Class;
    	idx : CARDINAL;
    	oneStudent : Student;
    	cid : ChanId;
    	res : OpenResults;
    BEGIN
    	cmpt14x := Class { Student { "Jane Doe", 0, 4.0 } BY 30 };
    
    	StreamFile.Open (cid, "cmpt14x.bin", write+raw, res);
    	IF res = opened THEN
    		RawIO.Write (cid, cmpt14x);
    	END;
    	StreamFile.Close (cid);
    
    	STextIO.WriteString ("The file should be");
    	SWholeIO.WriteCard (SIZE (cmpt14x), 0);
    	STextIO.WriteString (" LOCs long.");
    	STextIO.WriteLn;
    
    	idx := 17;
    	RndFile.OpenOld (cid, "cmpt14x.bin", read+raw, res);
    	IF res = opened THEN
    		RndFile.SetPos (cid,
    		  RndFile.NewPos (cid, idx-1, SIZE(Student),
    		    RndFile.StartPos (cid)));
    		RawIO.Read (cid, oneStudent);
    	END;
    	RndFile.Close (cid);
    END ClassList.
    
  5. [5] Rewrite the following FOR loop code snippet as a general LOOP. Don't worry about the rest of the module (IMPORT, VAR, etc.).
    FOR idx := 0 TO LENGTH (name)
      	DO
    		name[idx] := CAP (name[idx]);
    	END;
    
    idx := 0
    LOOP
    	name[idx] := CAP (name[idx]);
    	INC (idx);
    	IF idx > LENGTH (name) THEN
    		EXIT
    	END;
    END;
    
  6. [6] What is output from executing the following program module?
    MODULE ExceptTest;
    
    FROM M2EXCEPTION IMPORT
    	M2Exceptions, IsM2Exception, M2Exception;
    FROM EXCEPTIONS IMPORT
    	IsExceptionalExecution;
    FROM STextIO IMPORT
    	WriteString, WriteLn;
    FROM SRealIO IMPORT
    	WriteFixed;
    
    PROCEDURE FloorDivide (num, denom : REAL) : REAL;
    BEGIN
    	RETURN FLOAT ( INT (num) / INT (denom) );
    EXCEPT
    	IF IsExceptionalExecution() AND
    	  (M2Exception() = sysException) THEN
    		WriteString (" div-by-zero!");
    	END;
    END FloorDivide;
    
    BEGIN
    	WriteFixed ( FloorDivide ( 14.2, 5.9 ), 0, 0 );
    	WriteFixed ( FloorDivide (  1.5, 0.5 ), 0, 0 );
    	WriteFixed ( FloorDivide (  6.8, 2.1 ), 0, 0 );
    EXCEPT
    	IF IsExceptionalExecution() THEN
    		WriteString (" bummer!");
    		RETURN;
    	END;
    END ExceptTest.
    
    The output is " 2. div-by-zero! bummer!".