CS 117
Midterm 1
Ondich
Friday, April 26, 1996

This is a closed-book 62-point exam. Write your answers in the bluebook. If you want to put an answer on the exam itself, say so in the bluebook and put your name on the exam. Explain your answers clearly.

  1. (10 points) Write a function that takes two integers as parameters, and returns the larger of the two. Note that this function should contain no read, write, readln, or writeln calls.

  2. (10 points) What output does the following program produce? You may assume that it compiles and runs (it does--I tried it).
    
    program corn(output);
    
    var		a, b : integer;
    
    procedure wheat( a : integer );
    begin
    	a := 1
    end;
    
    procedure oats( var c : integer; d : integer );
    begin
    	c := 2;
    	d := 3
    end;
    
    procedure quatrotriticale( var e, f : integer );
    var		temp : integer;
    begin
    	temp := e;
    	e := e - f;
    	f := temp
    end;
    
    begin
    	a := 4;
    	b := 5;
    	wheat( b );
    	writeln( 'First: ', a, b );
    
    	a := 4;
    	b := 5;
    	oats( a, b );
    	writeln( 'Second: ', a, b );
    
    	a := 4;
    	b := 5;
    	quatrotriticale( b, a );
    	quatrotriticale( a, b );
    	writeln( 'Third: ', a, b )
    end.
    
    

  3. (10 points) A few questions about function/procedure parameters.

  4. (10 points) The folk singer Raffi often sings a song whose lyrics go like this:
    5 little ducks went out one day
    Over the hills and far away
    Mother duck said, "Quack quack quack quack"
    But only 4 little ducks came back
    
    4 little ducks went out one day
    ...
    
    1 little duck went out one day
    Over the hills and far away
    Mother duck said, "Quack quack quack quack"
    But none of the 5 little ducks came back
    

    Your task is to write a procedure to print out the lyrics of this song, starting with any number of ducks, like so:

    
    {
    	Ducks prints the lyrics of the song starting with
    	N ducks.
    }
    procedure Ducks( N : integer );
    

    By the way, the song does have a happy ending, but you may leave the poor mother duck in limbo.

  5. (4 points) What output does the following program fragment produce? You may assume that the variables i and j have been declared to have integer type.
    
    for i := 1 to 3 do
        for j := i to 3 do
            writeln( i, j );
    

  6. (6 points) Program errors can be classified as "compile-time" errors or "run-time" errors, depending on when the errors are first evident. Give an example of a program error that the compiler will catch (that is, a compile-time error). Give an example of a program error that the compiler will not catch, but that will appear when you run the compiled program.

  7. (2 points) I'm looking for a good book to read. What do you suggest?

  8. (10 points) Complete the following function.
    
    
    {
        IsCharInWord  returns true if the character ch is equal
        to any of the characters word[1], word[2], ..., word[length],
        and false otherwise.
    }
    function IsCharInWord( ch : char; word : charArray; length : integer ) : boolean;
    

    Note that this function should contain no read, write, readln, or writeln calls.

    The type charArray is defined by

    
    type     charArray = array[1..100] of char;