While Loop Exercises 1
Name: _________________________________
What is printed by each of these questions?
If a loop never finishes, or if it goes so long that an integer would go beyond
Integer.MAX_VALUE or Integer.MIN_VALUE, put infinite in the answer space.
- __________
int n = 0;
while (n < 4) {
n = n + 1;
}
System.out.println(n);
- __________
int n = 0;
int i = 0;
while (i < 4) {
if (i%2 == 0) {
n = n + 1;
}
i = i + 1;
}
System.out.println(n);
- __________
int n = 25;
while (n < 4) {
n = n + 1;
}
System.out.println(n);
- __________
int n = 25;
while (n >= 4) {
n = n + 1;
}
System.out.println(n);
|
- __________
int n = 0;
int m = 10;
while (n < m) {
n = n + 1;
m = m - 1;
}
System.out.println(n);
- __________
String s = "Now is a good time to laugh";
String result = "";
int n = 0;
while (s.indexOf(" ") != -1) {
result = result + s.substring(0, 1);
int blankPosition = s.indexOf(" ");
s = s.substring(blankPosition+1);
}
System.out.println(result);
- Show on the right what this prints.
int row = 0;
while (row < 4) {
int col = 0;
while (col < 3) {
System.out.print("*");
col++;
}
System.out.println();
row++;
}
|