Introduction to Programming
Sample Final Exam

The questions shown here are from an actual final exam given in an earlier year. Because the text used at that time was different, some questions found here will be unfamiliar to you. But go ahead and try some of these to get a sense of the kinds of questions I might ask.

1.    (18 points)  Short answer -- indicate the value of each expression:

      pos ( 't', 'Titration' ) =         pos ( 'ab', 'ball' ) =        

      copy ( 'Titration', 4, 6 ) =        

      line := 'The quick fox'; phrase := 'Now is the time';
      insert ( 'brown', line, 5 );  delete ( phrase, 5, 7 );

      line =                             phrase =                     

      message := 'This is hard to do';
      place := pos ( 'is hard', message );
      delete ( message, place, 8 );
      insert ( ' is easy', message, length(message) + 1 );

      message =                           


2.    (5 points)  Assume x has type INTEGER.  Under what circumstances will the
      following code cause the phrase "test point" to be printed?  Explain.

      IF ( x >= 0 ) AND ( x <= 10 )
      THEN
           CASE x OF
                0      : writeln ( 'point A' );
                1 .. 5 : writeln ( 'point B' );
                6 .. 8 : writeln ( 'point C' );
                9      : writeln ( 'point D' );
                10     : writeln ( 'point E' );
                ELSE     writeln ( 'test point' )
           END
      ELSE
           writeln ( 'point F' );


3.  (10 points)  Declare a record type variable called "employee" which has
    subfields "name" (string80) and "wage" (real).  Store the values 'Jones'
    and 7.75 in this record


4.  (10 points)  The array "scores" contains 25 student grades, in no
    particular order.  Complete the segment of code below to sort the array
    into descending order (from high values in the first positions to low
    values in the last positions).

    FOR primary := 
    DO FOR secondary :=
       DO IF 
          THEN
             BEGIN
                temp :=


             END


5.  (5 points)  What is a byte?  What is the relation between a kilobyte and
    1,000 bytes?


6.  (12 points)  Determine the output.

    PROGRAM exam1;
    VAR
       k, n, r : INTEGER;

    FUNCTION result
       ( j,
         k : INTEGER ) : INTEGER;
    BEGIN
       IF ( j < k + 1 ) AND ( k <> 0 )
       THEN result := ( j - k ) DIV k + 1
       ELSE result := j * j + k
    END;

    BEGIN
       k := 4;
       n := 0;
       r := result ( k, n + 4 );
       writeln ( r );
       r := result ( k, n ) + 4;
       writeln ( r );
       writeln ( result ( 5, 4 ) );
    END.


7.  (25 points)  Determine the output.

    PROGRAM exam2;
    TYPE
       t_type = array [ 1 .. 9 ] of INTEGER;
    VAR
       t : t_type;
       i, j, k : INTEGER;

    BEGIN
       k := 3;
       FOR i := 0 TO 2
       DO FOR j := 1 TO 3
          DO t [ i * 3 + j ] := i * j + k;
       FOR j := 1 TO 3
       DO FOR i := 1 TO 2
          DO write ( t [ i * 3 + j ] );
       writeln;
       writeln ( t [ 1 ] )
    END.


8.  (25 points)  Determine the output.

    PROGRAM exam3;                                     The file 'data.dat'
    TYPE                                               contains the following:
       a_type = array [ 1 .. 20 ] of INTEGER;          10
    VAR                                                20
       count, numin, last, position : INTEGER;         30
       infile : TEXT;                                  40
       a : a_type;                                     50
                                                       60
    BEGIN                                              70
       assign ( infile, 'data.dat' );                  80
       reset ( infile );                               90
       count := 0;                                     100
       readln ( infile, numin );                       110
       WHILE numin <> 99                               99
       DO BEGIN
             count := count + 1;
             a [ count ] := numin;
             readln ( infile, numin )
          END;
       FOR position := 1 TO 3
       DO BEGIN
             write ( a [ position ], a [ position + 3 ], a [ position + 6 ] );
             last := position + 9;
             IF last <= count
             THEN writeln ( a [ last ] )
             ELSE writeln
          END
    END.


9.  (25 points)  The array "names" contains a list of student names, while the
    array "scores" contains each student's corresponding exam results (i.e.,
    the results for student names[i] is stored in scores[i]).  The arrays
    contain "number of students" entries.  Write a short segment of code to
    perform the following tasks:
    a) Determine and display the average score.
    b) Display, for each student, their name, score, and whether their score is
       average, above average, or below average.


10.   (15 points)  Complete the following procedure which could be used to copy
      files.  Assume that the files consist of lines of strings.  The last line
      contains a sentinel of 'zzzzzzzzzz'.

      PROCEDURE copy file
           (         original,        { name of original file to be copied }
                     duplicate :      { name of duplicate file to be created }
                          string80 );

      CONST
           sentinel = 'zzzzzzzzzz';

      VAR
           input_line :
                string80;

      BEGIN
           assign (                )

           reset (                 )

           WHILE input_line <> sentinel
           DO
                BEGIN


      END;


11. (12 points)  Complete the following table showing all three representations
    for each number.

    decimal         binary         hexadecimal
                    1000011               
       27                                 
                                      3C  


12. (5 points)  Briefly explain why it is important always to close files.


13. (10 points)
    a) "A binary search can find a particular value in a 1000 element sorted
       array by checking at most 10 elements."  Briefly explain why the binary
       search is so efficient.

    b) Describe a linear search.  Identify one or more situations in which a
       linear search would be more appropriate than a binary search.


14. (10 points)  Write a code segment which will prompt for a single character.
     Continue to prompt until one of the following is entered:  'A', 'E', 'J',
    'S'


15. (13 points)  Write a code segment which will prompt for an integer value. 
    Continue to prompt until a valid integer value is entered.