CPSC 124, Fall 1996

Quiz Number 7


This is the seventh and final quiz given in CPSC 124: Introductory Programming, Fall 1996. See the information page for that course for more information.

The answers given here are sample answers that would receive full credit. However, they are not necessarily the only correct answers.


Question 1: What does it mean to say that a program is "robust"? Briefly discuss some of the guidelines for writing robust programs.

Answer: A robust program is one that can continue without crashing and give valid results, even if unexpected errors or exceptions occur. It is difficult for a program to anticipate all possible errors that might occur (especially errors in input data), and so writing robust programs is difficult. One guideline is to identify "preconditions" -- that is, conditions that must be true at a given point in the execution of a program if the program is to continue correctly -- and to check that the preconditions are satisfied. Another guideline is to use exception-handling to catch and deal with unexpected errors.


Question 2: Java has a predefined class called Throwable. What does this class represent? Why does it exist?

Answer: The class Throwable represents all possible objects that can be thrown when an exception occurs. That is, the object thrown by an exception must belong to the class Throwable or to one of its (many) subclasses. Like any class that is the superclass of a large family of classes, it exists to express the common properties of all those subclasses. (In this case, the common property is simply being "throwable.")


Question 3: Show the exact output produced by the following code segment. (This code segment assumes that the console has already been created.)

          char[][] pic = new char[6][6];
          for (int i = 0; i < 6; i++)
             for (int j = 0; j < 6; j++)
                if ( i == j  ||  i == 0  ||  i == 5 )
                   pic[i][j] = '*';
                else
                   pic[i][j] = '.';
          for (int i = 0; i < 6; i++) {
             for (int j = 0; j < 6; j++)
                console.put(pic[i][j]);
             console.putln();
          }

Answer: The output consists of six lines, with each line containing six characters. In the first line, i is 0, so all the characters are *'s. In the last line, i is 5, so all the characters are *'s. In each of the four lines in the middle, one of the characters is a * and the rest are periods. The output is

                       ******
                       .*....
                       ..*...
                       ...*..
                       ....*.
                       ******

It might help to look at the array items that are printed on each line:

         pic[0][0] pic[0][1] pic[0][2] pic[0][3] pic[0][4] pic[0][5] 
         pic[1][0] pic[1][1] pic[1][2] pic[1][3] pic[1][4] pic[1][5] 
         pic[2][0] pic[2][1] pic[2][2] pic[2][3] pic[2][4] pic[2][5] 
         pic[3][0] pic[3][1] pic[3][2] pic[3][3] pic[3][4] pic[3][5] 
         pic[4][0] pic[4][1] pic[4][2] pic[4][3] pic[4][4] pic[4][5] 
         pic[5][0] pic[5][1] pic[5][2] pic[5][3] pic[5][4] pic[5][5] 

Question 4: Suppose that grades is a two-dimensional array of integers that was created with the statement

int[][] grades = new int [30][4];

Suppose that grades has already been filled with data. The data consists of 4 test grades for each of 30 students, where grades[s][n] contains the grade for student number s on test number n. Write a code segment that will print out the average grade for each student. This average can be obtained by adding up that student's test grades and dividing by 4.0.

Answer: It's not clear what this problem means when it says to "print out" the test grades. Using System.out would be acceptable. So would using the type of console-based output that was used in most of the examples in this course. There are many possible answers to this problem. Sometimes, a miminalist answer is best:

           for (int s = 0; s < 30; s++) {
              double average = (grades[s][0] + grades[s][1] + grades[s][2] + grades[s][3]) / 4.0;
              System.out.println("Average for student " + i + " is " + average);
           }

Of course, you could also use a loop to add up the grades for the s-th student:

           for (int s = 0; s < 30; s++) {
              int total = 0; // total grade;
              for (int n = 0; n < 4; n++)
                 total = total + grades[s][n];
              double average = total / 4.0;
              System.out.println("Average for student " + i + " is " + average);
           }

David Eck