What does this code print?
int n = 2;
while (n < 9) {
System.out.print(n + " ");
n += 3;
}
2 5 8
What does this code print?
for (int k = 4; k <= 10; k += 2) {
System.out.print(k + " ");
}
4 6 8 10
What value prints?
int total = 0;
for (int n = 2; n <= 6; n += 2) {
total += n;
}
System.out.println(total);
12
What prints?
String word = "planet";
System.out.println(word.substring(1, 4));
lan
How many times does the print statement execute?
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 4; col++) {
System.out.print("@");
}
}
12
What prints?
int n = 7;
while (n > 1) {
n -= 2;
System.out.print(n + " ");
}
5 3 1
What does this code print?
for (int k = 1; k <= 8; k++) {
if (k % 3 == 2) {
System.out.print(k + " ");
}
}
2 5 8
Write a for loop that prints 24 20 16 12 8
for (int n = 24; n >= 8; n -= 4) {
System.out.print(n + " ");
}
What prints?
String word = "COMPUTER";
String result = "";
for (int i = 1; i < word.length(); i += 2) {
result += word.substring(i, i + 1);
}
System.out.println(result);
OPTR
What prints?
for (int row = 1; row <= 3; row++) {
for (int col = 0; col < 2; col++) {
System.out.print(row + 1);
}
System.out.println();
}
22
33
44
This loop should print 2 4 6 8 and stop. Rewrite it correctly.
int n = 2;
while (n <= 8) {
System.out.print(n + " ");
}
need to add:
n += 2;
Fix only the for-loop so the output is 12 9 6 3.
for (int k = 12; k > 3; k -= 3) {
System.out.print(k + " ");
}
for (int k = 12; k >= 3; k -= 3)
Write a loop from 1 through 8. Print each number followed by " small" if it is less than 5, or " large" otherwise.
for (int n = 1; n <= 8; n++) {
if (n < 5) {
System.out.println(n + " small");
} else {
System.out.println(n + " large");
}
}
What prints?
String word = "robot";
while (word.length() > 1) {
word = word.substring(1);
System.out.println(word);
}
obot
bot
ot
t
Write nested loops to print this pattern:
####
####
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 4; col++) {
System.out.print("#");
}
System.out.println();
}
Rewrite this for loop as a while loop with the same output.
for (int n = 15; n >= 3; n -= 3) {
System.out.print(n + " ");
}
int n = 15;
while (n >= 3) {
System.out.print(n + " ");
n -= 3;
}
Rewrite this while loop as a for loop that has the same output.
int k = 2;
while (k < 20) {
System.out.print(k + " ");
k *= 2;
}
for (int k = 2; k < 20; k *= 2) {
System.out.print(k + " ");
}
Write code to find the sum of all integers from 1 through 20 that are divisible by 3. Print the sum.
int sum = 0;
for (int n = 1; n <= 20; n++) {
if (n % 3 == 0) {
sum += n;
}
}
System.out.println(sum);
Using a loop, build and print the reverse of word.
String reversed = "";
for (int i = word.length() - 1; i >= 0; i--) {
reversed += word.substring(i, i + 1);
}
System.out.println(reversed);
What is my sister's name?
Bonus: Where does she work?
Megan Schullo
Bonus: Kickapoo High School
Find the final values of a and b.
int a = 9;
int b = 3;
while (b != 0 && a / b > 1) {
a -= 2;
b--;
}
a = 3, b = 0
Find the final values of a and b.
int a = 2;
int b = 1;
for (int k = 1; k <= 3; k++) {
a = a + b;
b = a - k;
}
a = 8, b = 5
What value prints
int number = 5832;
int count = 0;
while (number > 0) {
number /= 10;
count++;
}
System.out.println(count);
*Bonus: What does the algorithm count?
4
*Bonus: It counts the decimal digits in this positive integer
Write a loop that removes one # each iteration and prints the updated string after every removal.
while (text.indexOf("#") != -1) {
int p = text.indexOf("#");
text = text.substring(0, p) + text.substring(p + 1);
System.out.println(text);
}
Write nested loops to print this pattern:
1
23
456
int number = 1;
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= row; col++) {
System.out.print(number);
number++;
}
System.out.println();
}