printable version
Quiz 1
[
1]
[
2]
[
3]
[
4]
Problem Q1.1.
[8 pts]
Sketch the final appearance of the window when the
computer completes the program fragment below.
Turtle mystery = new Turtle(100, 50);
int toDraw = 4;
while(toDraw > 0) {
mystery.forward(25);
mystery.right(60 - 60 * toDraw * (toDraw % 2));
toDraw = toDraw - 1;
}
mystery.hide();
Problem Q1.2.
[6 pts]
What Java condition belongs as the while condition (where
the comment is below) so that the loop will continue until
sum is
no longer between −1 and 1?
Random seq;
seq = new Random();
double sum;
sum = 0.0;
while(/* condition to write */) {
sum = sum + seq.nextDouble() - seq.nextDouble();
}
sum >= -1 && sum < 1
Problem Q1.3.
[6 pts]
In the above program fragment, draw an oval around each variable
initialization and a rectangle around each variable
declaration.
The lines Random seq;
and
double sum;
are declarations and so should have rectangles around them.
The lines
seq = new Random();
and sum = 0.0;
are initializations and so should have ovals around them.
Problem Q1.4.
[10 pts]
List all values taken on by the variables q, r,
and s as a computer executes the below Java fragment.
int q = 0;
int r = 1;
int s = 2;
while(q < 10) {
q = r;
r = 2 * r;
s = s + r;
}
q: |
0, |
1, |
2, |
4, |
8, |
16 |
r: |
1, |
2, |
4, |
8, |
16, |
32 |
s: |
2, |
4, |
8, |
16, |
32, |
64 |