CS 10: Introduction to Computer
Programming
In-Class Examination I
Open-Book, 75 minutes, Spring 1997
General Instructions
-
Read each question carefully.
-
Make sure that you clearly understand a question before answering it.
-
Please put your answer to each question on its page.
-
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.
Name:
Perm:
Question
|
Value
|
Score
|
1
|
10
|
|
2
|
10
|
|
3
|
10
|
|
4
|
10
|
|
5
|
10
|
|
6
|
10
|
|
7
|
10
|
|
8
|
10
|
|
9
|
10
|
|
10
|
10
|
|
Total
|
100
|
|
1. What is the output of the following code fragment?
while ( i*i < 5) {
g.drawString("i = " + y, 25, y);
y += 15;
i += 3;
}
Solution:
i = 10
i = 25
2. Convert each of the following
using an assignment operator (e.g., +=, -=, *=, /=, %=). If this
cannot be done, say "It can't be done".
-
xyz = xyz + zyx; Solution:
xyz += zyx;
-
xyz = zyx + xyz; Solution:
xyz += zyx;
-
x = 3 - x;
Solution: none.
-
x = x*a + 3;
Solution: none.
-
xyz = (a - 5)*xyz; Solution:
xyz *= a - 5;
3. Convert
the following while loop
fragment to an equivalent for
loop. If it can't be done,
say "It can't be done".
double x = 0.0;
while ( x <= 1.1 ) {
sum += x;
x += 0.2;
}
Solution:
for (double x = 0.0; x
<= 1.1; x += 0.2)
sum += x;
4. Convert the following
for loop
fragment to an equivalent while
loop. If it can't be done,
say "It can't be done".
for (int j = 100; j*j > 40; j -= 10)
{
sum += j % 3;
j += 5;
}
Solution:
int j = 100;
while (j*j > 40) {
sum += j % 3;
j -= 5;
}
5. Convert the following code
using a switch statement.
The variable x is
an int.
if (x >= 0 && x < 250)
// use a switch for the part that starts here
if (x < 50)
g.setColor(Color.black);
else if (x < 100)
g.setColor(Color.red);
else if (x < 150)
g.setColor(Color.blue);
else if (x < 200)
g.setColor(Color.yellow);
else
g.setColor(Color.green);
// and ends here
Solution:
if (x >= 0 && x < 250)
switch (x / 50) {
case 0:
g.setColor(Color.black);
break;
case 1:
g.setColor(Color.red);
break;
case 2:
g.setColor(Color.blue);
break;
case 3:
g.setColor(Color.yellow);
break;
default:
g.setColor(Color.green);
}
6. What is the output of the following
code fragment?
int sum = 0, i = 0, y = 10;
while (++i < 6) {
switch (i*i % 4) {
case 0:
continue;
case 1:
sum += 2;
case 2:
sum += i*i % 3;
default:
sum += i*i % 3;
}
g.drawString("sum
= " + sum, 25, y);
y += 15;
}
Solution:
sum = 4
sum = 6
sum = 10
7. Convert the following code
fragment to an equivalent one that does not use a continue.
Draw a picture of the result.
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10;
j++)
if (j < i)
continue;
else
g.fillRect(15*j, 15*i, 10, 10);
Solution:
for (int i = 0; i < 10; i++)
for (int j = i; j < 10;
j++)
g.fillRect(15*j, 15*i, 10, 10);
8. Convert the following nested
if statements
to one that uses boolean operators. If it can't be done, say "It
can't be done".
if (john >= 16)
if ( mary >= 16)
if (oscar != 20.0)
sum = term;
if (mary <= 5)
sum = term;
else
sum = factor;
Solution:
sum = term;
Solution 2 (worth 3 points):
if (john >= 16 &&
mary >= 16 && oscar != 20.0)
// This statement nullifies any affect
of the previous statement.
// Also, the question calls for only
1 if statement.
if (mary <= 5)
sum = term;
else
sum = factor;
9. What is the output of the following
code fragment?
int i = 0, y = 10;
do {
g.drawString("i =
" + i, 25, y);
y += 15;
}
while (i++ < 0);
Solution:
10. Give the Java code for an applet
that draws a solid blue rectangle of size 35 X 35 pixels wherever the user
clicks on the applet. The applet should be empty until a mouse click
occurs.
Solution:
// An applet for drawing a solid blue square
at mouseDown coordinates
import java.awt.*;
import java.applet.*;
public class Temp extends Applet {
boolean first = true;
int X, Y;
// coordinates of cursor at mouseDown
public void paint(Graphics
g) {
g.setColor(Color.blue);
if (!first)
g.fillRect(X, Y, 35, 35);
}
public boolean mouseDown(Event
e, int x, int y) {
X = x;
Y = y;
first = false;
repaint();
return true;
}
}