CS 10: Introduction to Computer Programming

In-Class Examination 1

Closed-Book, 50 minutes, Fall 1997
 

General Instructions


Question  Value 
1  25 
2  25 
3  25 
4  25 
Total 100 

1. What is the value of variables i, j, and k after the statements below have been executed?

2. Write java statement[s] that assign 7 to int variable j, if the value of int variable i is evenly divisible by 2 and 3 and is evenly divisible by neither 5 nor 7, and assigns -7 to int variable j otherwise.  Do not use boolean operators.
 

3. Convert the following code fragment to an equivalent one that uses for statements in place of the while statements.  If this cannot be done, explain why.



    int i = 1, limit = 8, n;
    double x = i, term, e2x;

    System.out.println("Begin computation of various values of e");
    while (i <= 10) {
        n = 1;
        x *= -i;
        e2x = term = 1.0;
        System.out.println(i + ": x = " + x);

        while (n <= limit) {
            term *= x/n;
            e2x += term;
            System.out.println("    " + n + ": term = " + term + ", e2x = " + e2x );
            n++;
        }
        i++;
        System.out.println("    End computation of e to " + x);
    }
    System.out.println("End computation of various values of e");


4. Complete the following skeletal applet so that it produces a "bull's eye".  The bull's eye is centered on pixel (150, 150).  The inner circle is of radius 40 pixels, and is black.  The middle ring is white and is 40 pixels wide.  The outer ring is black and is 40 pixels wide.