CS 10: Introduction to Computer Programming
In-Class Examination 1
Closed-Book, 75 minutes, Winter 1998
General Instructions
-
Before you answer any questions, write your name and perm number.
-
Read each question carefully. Make sure that you clearly understand
each question before answering it.
-
Put your answer to each question on its own page.
-
Use a pen to write your answer. 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 |
20 |
2 |
20 |
3 |
20 |
4 |
20 |
5 |
20 |
Total |
100 |
1. What is the value of variables i, j, and k
at point A) and B) below?
int i = -7, j = i*i, k =
j%5;
// A) what is the value
of i, j, and k at this point?
i *= i + 1;
j %= i-- + 5;
k /= i = j + ++i;
// B) what is the value
of i, j, and k at this point?
2. In
the applet skeleton below, interpret the int variables x
(horizontal coordinate) and y (vertical coordinate) to be the coordinates
of a point. Complete the following skeletal applet so that
it paints a filled square whose upper left corner
is at coordinates (100, 100). The square should be 100 X 100, and
colored:
-
black if the point (x, y) is
somewhere in the square whose upper left corner has coordinates (100, 100)
and whose lower right corner has coordinates (200, 200).
-
red otherwise.
Assume that variables x and y
already have values.
import java.awt.*;
import java.applet.*;
public class test extends Applet {
public void paint(Graphics
g) {
int x,y;
}
}
3. Convert
the following sequence of Java statements to an equivalent sequence without
for statements. You may use while statements.
If this cannot be done, explain why.
for (int i = 1; i < 100;
i *= -3)
for (int j = 1; j < 100; j *= -2) {
g.fillRect(X + j*5, Y + i*5, 5, 5);
}
4. Complete
the following skeletal applet so that it computes, into a variable called
sum, the sum of all numbers between 10 and 60 that are not evenly
divisible by 2, 3, 5, and 7. Output the value of sum somewhere
on the applet using the drawString method.
import java.awt.*;
import java.applet.*;
public class test extends Applet {
public void paint(Graphics
g) { ... }
}
5. Complete
the following skeletal applet so that it paints 2 triangular stacks of
circles, as shown (on the monitor during the exam). Each circle is
10 pixels in diameter and is separated by 2 pixels from the circles around
it. The triangles are identical except the left one starts at the
left side of the applet; the right triangle starts at horizontal coordinate
200. Each triangle has 15 rows. The triangles are colored the
same: Circles in the same column have the same amount of red.
Each circle has no green and no blue. The amount of red in a column
is 255/15 more than the column to its left. The leftmost column has
no red.
import java.awt.*;
import java.applet.*;
public class test extends Applet {
public void paint(Graphics
g) { ... }
}