CS 10: Introduction to Computer Programming
In-Class Examination 2
Closed-Book, 50 minutes, Fall 1997
General Instructions
-
Before you answer any questions, write your name, perm number, and
discussion section on your answer booklet.
-
Read each question carefully.
-
Make sure that you clearly understand a question before answering it.
-
Please put your answer to each question on its own page in your blue-book.
-
Answers that do not show work will not get partial credit.
-
You may wish to work out an answer on scratch paper before writing it on
your answer page; answers that are difficult to read may lose points for
that reason.
Question |
Value |
1 |
15 |
2 |
15 |
3 |
15 |
4 |
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.
Solution:
(int) (Math.random()*(end
- start + 1)) + start
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.
Solution:
j = (i % 6 == 0 &&
i % 5 !=0 && i % 7 != 0) ? 7 : -7;
Alternatively,
if (i % 6 == 0 &&
i % 5 !=0 && i % 7 != 0)
j = 7;
else
j = -7;
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);
Solution:
switch (tool) {
case 1:
g.drawLine(x, y, x + width, y + height);
break;
case 2:
g.drawRect(x, y, width, height);
break;
case 3:
g.fillRect(x, y, width, height);
break;
case 4:
g.drawOval(x, y, width, height);
break;
case 5:
g.fillOval(x, y, width, height);
break; // unnecessary but prudent
}
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.
Solution:
public boolean implies(boolean
p, boolean q) {return !p || q;}
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.
Solution:
public float summer(int
data[], int top) {
float sum = (float) 0.0;
for (int i = 1; i < top && i < data.length; i +=2)
sum += data[i];
return sum;
}
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.
Solution:
public void maximizer(int
table[][], int max[]) {
for (int i = 0; i < table.length; i++) {
max[i] = table[i][0];
for (int j = 1; j < table[i].length; j++)
if (max[i] < table[i][j])
max[i] = table[i][j];
}
}