Professional Documents
Culture Documents
e.g.
float average; // Declare a variable called average, of type float
int x = 5;
float y=5f;
String address;
String name = “Iasone”;
x = x + 5;
a++;
a--;
y += 5;
z *= 3;
d=x % 3;
// Declare a string variable called address and assign “1138 Kessel Drive” to it.
/* Declare a variable to store floating pointer numbers and give that variable a name of Buzz
and assign 1.138 to it.
*/
// add g1, g2, and g3 and assign that value to a variable called average
// print out on one line “The average of “ followed by the values of g1, g2, and g3 separated by “,”
1 String s;
2 int n;
3 float d;
4 s = “hello”;
5 n = 5;
6 n = 6;
7 d = 2.1f;
8 s = s+“goodbye”;
9 n = 4;
s n d
1 “” Not defined Not defined
2 “” 0 Not defined
3
4
5
6
7
8
9
1 int a;
2 int b;
3 int c = 5;
4 float f = 2.5f;
5 a = c - 2;
6 b = a * c;
7 f /= 2;
8 a = b + c;
9 b += c;
10 b--;
==
!=
>
>=
<
<=
8. In the lines of code marked a to e, indicate which lines are true or false or in
error:
a. x >= k
b. k > zzz
c. x = 10
int n = 100; int b = 7;
if (n >= 100) {
if (n / b > 10) {
if (b % 3 == 0) {
System.out.println("first");
} else if (b % 3 == 1) {
System.out.println("second");
} else {
System.out.println("third");
} else {
if (b % 2 == 0) {
System.out.println("fourth");
} else {
System.out.println("fifth");}
} else {
System.out.println("sixth");
While Loops
11. Fill in the blanks in the following code to produce the desired outcome:
(10 points)
int c = 0;
while (______) {
System.out.println(c);
c++;
}
int c = ___
while (______) {
System.out.println(c);
c=c+2;
}
int c = ___
while (______) {
c+=2;
System.out.println(c);
}
int _________
while (______) {
System.out.println(c);
c--;
}
int d = _____
do {
System.out.println(d);
d++;
} while (d<1000);
int x = _____
do {
x--; System.out.println(x);
} while (______);
11111
22222
33333
44444
int x=1;
int y=1;
while(y<=4) {
while(_____) {
System.out.print(y);
x++;
}
System.out.println(“”);
y++;
___________
}
11 21 31 41
12 22 32 42
13 23 33 43
int x=1;
int y=1;
while(_____) {
while(_____) {
System.out.print(x+”“+y+” “);
x++;
}
System.out.println(“”);
y++;
____________
}
int c = 0;
float b = 0.2f;
int e = 3;
float res = 1;
while (c < e) {
res *= b;
c++;
}
13. Look at the Bug Run project (provided in the lecture notes folder).
In this program the bug rotated from the left side of the
screen to the right side of the screen and then stopped.
Add a second while loop to move the bug back to the left
side of the screen.