NEW
Font size
WorksheetsJava Code Output Quiz
Total questions: 30
Worksheet time: 15mins
What will this code print? int x = 0; if (x == 0) { System.out.println("Zero"); } else if (x > 0) { System.out.println("Positive"); } else { System.out.println("Negative"); }
Zero
Positive
Negative
Nothing
What does this code print? int i = 0; while (i 0; i--) { System.out.print(i + " "); }
1 2 3
3 2 1
3 2
2 1 0
How many times does this loop run? int x = 5; while (x > 0) { x--; } System.out.println(x);
4
5
6
Infinite
What is the output of this code? for (int i = 1; i 1; i--) { if (i == 3) break; System.out.print(i + " "); }
5 4 3
5 4
4 3 2
5 4 2
What is the output of this nested loop? for (int i = 1; i 0) { for (int j = 0; j 1) { System.out.print(x + " "); x--; }
4 3 2
4 3 2 1
3 2 1
4 3
What will this code output? int count = 0; for (int i = 1; i <= 4; i++) { if (i % 3 == 0) count++; } System.out.println(count);
1
2
3
4
What is the output of this code? int i = 0; while (i < 5) { if (i % 2 == 0) { System.out.print(i + " "); } i++; }
0 2 4
1 3 5
0 2
2 4
What is the output of this code? int product = 1; for (int i = 1; i <= 4; i += 2) { product = i; } System.out.println(product);
3
6
12
9
What will this code print?
int x = 0;
while (x < 3) {
x++;
if (x == 2) continue;
System.out.print(x + " ");
}
1 2 4
1 3
2 3
1 2
What is the result of this loop?
for (int i = 0; i < 4; i++) {
System.out.print(i 2 + " ");
}
0 2 4 6
0 1 2 3
2 4 6 8
0 2 4
What is the result of this loop?
for (int i = 0; i < 4; i++) {
System.out.print(i 2 + " ");
}
0 2 4 6
0 1 2 3
2 4 6 8
0 2 4
How many times does "Hi" print?
int i = 1;
while (i <= 5) {
System.out.println("Hi");
i += 2;
}
2
3
4
5
What will this code output?
for (int i = 5; i > 1; i--) {
if (i == 3) break;
System.out.print(i + " ");
}
5 4 3
5 4
4 3 2
5 4 2
What is the output of this nested loop?
for (int i = 1; i <= 2; i++) {
int j = 0;
while (j < 2) {
System.out.print(i + j + " ");
j++;
}
}
1 2 2 3
1 1 2 2
1 0 2 1
0 1 1 2
What will this code print?
int sum = 0;
for (int i = 1; i <= 3; i++) {
sum += i;
System.out.print(sum + " ");}
1 3 6
1 2 3
3 6 9
0 1 3
What is the output of this code?
int i = 0;
while (i <= 6) {
System.out.print(i + " ");
i += 3;
}
0 3 6
0 1 2 3 4 5 6
3 6 9
0 3
What will this loop do?
for (int i = 0; i < 6; i += 2) {
if (i == 4) break;
System.out.print(i + " ");
}
0 2
0 2 4
2 4 6
0 2 4 6
What will this loop do?
for (int i = 0; i < 6; i += 2) {
if (i == 4) break;
System.out.print(i + " ");
}
0 2
0 2 4
2 4 6
0 2 4 6
How many times does "Run" print?
int i = 0;
for (; i < 5; i++) {
System.out.println("Run");
}
4
5
6
Infinite
What is the output of this code?
int x = 3;
while (x > 0) {
for (int j = 0; j < 2; j++) {
System.out.print("");
}
x--;
}
""
"" "" "" ""
3 2 1
Infinite Loop
What will this code print?
for (int i = 0; i < 3; i++) {
System.out.print(i + " ");
if (i == 1) break;
}
0 1
0 1 2
1 2
0
What is the result of this loop?
int x = 4;
while (x > 1) {
System.out.print(x + " ");
x--;
}
4 3 2
4 3 2 1
3 2 1
4 3
What will this code output?
int count = 0;
for (int i = 1; i <= 4; i++) {
if (i % 3 == 0) count++;
}
System.out.println(count);
1
2
3
4
What is the output of this code?
int i = 0;
while (i < 5) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
i++;
}
0 2 4
1 3 5
0 2
2 4
What will this loop print?
for (int i = 1; i <= 2; i++) {
int j = 0;
while (j < i) {
System.out.print("");
j++;
}
System.out.println();
}
"" ""
1
2
1 2
* **
What is the output of this code?
int product = 1;
for (int i = 1; i <= 4; i += 2) {
product = i;
}
System.out.println(product);
3
6
12
9
What will this code print when executed?
int x = 5;
for (int i = 0; i < x; i++) {
while (x > 2) {
x--;
if (x == 3) break;
System.out.print(x + " ");
}
}
4 2
4
4 2 4 2 4
Nothing
What will this code print when executed?
int x = 5;
for (int i = 0; i < x; i++) {
while (x > 2) {
x--;
if (x == 3) break;
System.out.print(x + " ");
}
}
4 2
4
4 2 4 2 4
Nothing
How many times will "Loop" be printed?
int count = 0;
for (int i = 1; i <= 4; i++) {
while (count < i) {
System.out.println("Loop");
count++;
}
}
4
6
10
8
What is the output of this nested loop structure?
int total = 0;
for (int i = 0; i < 3; i++) {
int j = 0;
while (j <= i) {
total += j;
j++;
}
}
System.out.println(total);
```
3
4
9
0
