CS 10: Introduction to Computer Programming

In-Class Examination 2

Closed-Book, 50 minutes, Fall 1997
 

General Instructions


Question  Value 
15 
15 
15 
15 
5 20
6 20
Total 100 

1. Write a java expression whose value is a random integer in the set [start, start+1, start+2, ..., end], for int variables start and end.  Assume that end > start.  If this cannot be done, explain why.

2. Write a java statement that assigns 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 nest if statements.

3. Convert the following code fragment to an equivalent one that uses a switch statement.  If this cannot be done, explain why.



        // apply tool
        if (tool == 1)
            g.drawLine(x, y, x + width, y + height);
        else if (tool == 2)
            g.drawRect(x, y, width, height);
        else if (tool == 3)
            g.fillRect(x, y, width, height);
        else if (tool == 4)
            g.drawOval(x, y, width, height);
        else if (tool == 5)
            g.fillOval(x, y, width, height);


4. Write a java method, implies, that takes 2 boolean (true or false) arguments, p and q, and returns true if p is false or q is true.

5.  Write a java method, summer, that takes an argument, data,  that is an int array, and an argument top, that is an int, and returns, as a float, the sum of the odd elements of the array, data, (which starts with element 0) whose indices are less than top.

6. Write a java method, maximizer, that takes 2 int arguments: a doubly-subscripted array, table, and an array, max.  The method puts the maximum element of row i of table into element i of max.