CS 10: Introduction to Computer Programming

In-Class Examination 1

Closed-Book, 75 minutes, Spring 1999

General Instructions



1. Consider the following code fragment.

        int i, j, k, m;

        // some Java statements are omitted that assign values to i, j, k, & m.

        // Your Java statement goes here



Create a single Java statement that assigns new values to i, j, k, and m as follows (in the order indicated):

  1. Increment i's value by 1
  2. Multiplies  j by the sum of i's new value and m's current value
  3. Assigns to k the remainder of its current value when divided by j's new value
  4. Decrements m by 1.
2. Give Java statements for computing the sum of the odd numbers squared from 1 to 2001. That is, 1 + 9 + 25 + ... + 2001*2001.

3. Given the applet skeleton below, fill in the paint method so that it renders the following picture:

In the picture, painting is inset 40 pixels from the left and the top.  Each black square has an edge length of edg; there are num such squares on the diagonal and anti-diagonal (i.e., both diagonals).

4. In the applet above, add Java statements to the paint method so that when the mouse is released inside one of the black squares, that square is made blue.  Otherwise, nothing is done.

5.  Give Java statements to produce the following "sum":  Consider each number from 1 to 1000.  If the number when divided by 10 has a remainder of 2, 3, 5, or 7, add the remainder to the sum.  If the remainder is 0, 1, or 9, subtract the remainder from the sum. If the remainder is something else, add 1 to the sum.  Your solution must use a switch statement to get full credit.