Java Find Output Questions and Answers - Loops

Here are 10 Java code snippets designed to test your understanding of Java Loops. Each question includes a code snippet and multiple-choice options for the output. After you complete the test, you can check your score, the correct answer, and an explanation.

These questions cover a variety of loop types and behaviors, including for, while, and do-while loops, as well as conditional statements to manipulate loop execution flow.

1. What will be the output of the following Java code?

for (int i = 0; i < 5; i++) {
    if (i == 3) break;
    System.out.print(i + " ");
}
a) 0 1 2 3
b) 0 1 2
c) 0 1 2 3 4
d) 0 1 2 4

2. What will be the output of the following Java code?

int i = 0;
while (i < 5) {
    i++;
    if (i == 3) continue;
    System.out.print(i + " ");
}
a) 1 2 4 5
b) 1 2 3 4 5
c) 2 3 4 5
d) 1 2 3 4

3. What will be the output of the following Java code?

int i = 5;
do {
    System.out.print(i + " ");
    i--;
    } while (i > 0);
a) 5 4 3 2 1 0
b) 5 4 3 2 1
c) 5
d) 4 3 2 1 0

4. What will be the output of the following Java code?

for (int i = 0; i <= 10; i += 2) {
    System.out.print(i + " ");
}
a) 0 1 2 3 4 5 6 7 8 9 10
b) 0 2 4 6 8 10
c) 0 2 4 6 8
d) 2 4 6 8 10

5. What will be the output of the following Java code?

int count = 1;
for (int i = 5; i > 0; i--) {
    count *= i;
}
System.out.println(count);
a) 15
b) 120
c) 24
d) 5

6. What will be the output of the following Java code?

for (int i = 1; i <= 5; i++) {
    if (i % 3 == 0) {
        System.out.println("i is a multiple of 3");
        break;
    }
}
a) i is a multiple of 3
b) No output
c) Error
d) i is a multiple of 3 repeated five times

7. What will be the output of the following Java code?

int num = 5;
int factorial = 1;
while (num > 1) {
    factorial *= num;
    num--;
}
System.out.println(factorial);
a) 120
b) 24
c) 5
d) 0

8. What will be the output of the following Java code?

int total = 0;
for (int num = 1; num <= 10; num++) {
    if (num % 4 == 0) continue;
    total += num;
}
System.out.println(total);
a) 52
b) 50
c) 54
d) 49

9. What will be the output of the following Java code?

int x = 1;
for (; x <= 5; x++) {
    System.out.print(x + " ");
    if (x == 3) break;
}
a) 1 2 3
b) 1 2 3 4 5
c) 1 2
d) 1 2 3 4

10. What will be the output of the following Java code?

for (int i = 0; i < 5; i++) {
    if (i == 2 || i == 4) continue;
    System.out.print(i + " ");
}
a) 0 1 2 3 4
b) 0 1 3
c) 0 1 3 4
d) 0 1 2 3

Comments