Code Snippets-based MCQs
Loops
Table of contents
Easy Level MCQs - Single Loop
-
- Given the following Java code snippet, what will be printed to the console?
for (int i = 1; i <= 5; i++) { System.out.print(i + " "); }
Answer
1 2 3 4 5
-
What will be the output of the following Java code?
int count = 0; while (count < 3) { System.out.print("Hi "); count++; }
Answer
Hi Hi Hi
-
After executing the following Java code, what will be the value of
sum
?int sum = 0; for (int i = 1; i <= 4; i++) { sum += i; }
Answer
10
-
What will the following Java code print?
int i = 5; do { System.out.print(i + " "); i--; } while (i > 0);
Answer
5 4 3 2 1
-
How many times will “Java” be printed by the following code?
int i = 0; while (i < 5) { System.out.println("Java"); i += 2; }
Answer
3
-
What is the output of the following Java code?
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 2; j++) { System.out.print(i * j + " "); } }
Answer
1 2 2 4 3 6
-
Determine the output of this Java code snippet.
int i = 0; do { if (i % 2 == 0) { System.out.print(i + " "); } i++; } while (i < 5);
Answer
0 2 4
-
What will be printed by the following Java code?
for (int i = 3; i > 0; i--) { System.out.print(i + " "); }
Answer
3 2 1
-
After executing the following Java code, what is the value of
product
?int product = 1; for (int i = 1; i <= 3; i++) { product *= i; }
Answer
6
-
What is the output of this Java code?
int i = 1; while (i <= 3) { System.out.print("Number: " + i + " "); i++; }
Answer
Number: 1 Number: 2 Number: 3
-
How many times will “Loop” be printed by this Java code?
int i = 0; do { System.out.println("Loop"); i++; } while (i < 2);
Answer
2
-
Determine the output of the following Java code.
for (int i = 0; i < 5; i += 2) { System.out.print(i + " "); }
Answer
0 2 4
-
What will be printed by this Java code?
int sum = 0; int i = 1; while (i <= 3) { sum += i; i++; } System.out.println(sum);
Answer
6
-
After executing the following Java code, what will be the output?
int i = 5; do { System.out.print(i + " "); i -= 2; } while (i > 0);
Answer
5 3 1
-
What will be the output of this Java code?
for (int i = 1; i <= 3; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); }
Answer
*
* -
How many times will “Hello” be printed in the following Java code?
int i = 0; while (i < 4) { System.out.println("Hello"); i++; }
Answer
4
-
Determine the output of this Java code snippet.
int total = 0; for (int i = 1; i <= 5; i++) { if (i % 2 == 0) { continue; } total += i; } System.out.println(total);
Answer
9
-
What will the following Java code print?
int i = 1; do { System.out.print(i + " "); i *= 2; } while (i < 10);
Answer
1 2 4 8
-
After executing this Java code, what will be the value of
count
?int count = 0; for (int i = 0; i < 5; i++) { if (i % 2 == 0) { count++; } }
Answer
3
-
What will be printed by the following Java code?
for (int i = 1; i <= 3; i++) { for (int j = 3; j >= i; j--) { System.out.print(j + " "); } System.out.println(); }
Answer
3 2 1
3 2
3 -
How many times will “Test” be printed in this Java code?
int i = 0; do { System.out.println("Test"); i++; } while (i < 0);
Answer
1
-
Determine the output of this Java code.
for (int i = 1; i <= 5; i++) { if (i == 3) { break; } System.out.print(i + " "); }
Answer
1 2
-
What will the following Java code output?
int i = 10; while (i > 0) { System.out.print(i + " "); i -= 3; }
Answer
10 7 4 1
-
How many times will the inner loop execute in total in the following Java code?
for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { System.out.println("Inner Loop"); } }
Answer
6
-
What will be printed by this Java code?
int i = 1; do { if (i == 3) { i++; continue; } System.out.print(i + " "); i++; } while (i <= 5);
Answer
1 2 4 5
-
Determine the output of this Java code snippet.
for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { System.out.print(i + j + " "); } }
Answer
0 1 1 2
-
What will be the final value of
result
after executing this Java code?int result = 1; int i = 1; while (i <= 3) { result *= i; i++; }
Answer
6
-
How many times will “Executing” be printed by the following Java code?
int i = 0; while (true) { System.out.println("Executing"); if (i == 2) { break; } i++; }
Answer
3
-
What will be the output of this Java code?
for (int i = 1; i <= 3; i++) { System.out.print("Outer" + i + " "); for (int j = 1; j <= 2; j++) { System.out.print("Inner" + j + " "); } }
Answer
Outer1 Inner1 Inner2 Outer2 Inner1 Inner2 Outer3 Inner1 Inner2
-
Determine the output of the following Java code.
int i = 0; do { System.out.print(i + " "); i++; } while (i < 3);
Answer
0 1 2
Slightly harder MCQs - Single Loop
-
What will be the output of the following code?
int sum = 0; for (int i = 1; i <= 5; i++) { sum += i; } System.out.println(sum);
Answer
15
-
What will be the output of the following code?
int i = 0; while (i < 3) { i++; } System.out.println(i);
Answer
3
-
What will be the output of the following code?
int i = 0; do { i++; } while (i < 0); System.out.println(i);
Answer
1
-
What will be the output of the following code?
int product = 1; for (int i = 1; i <= 4; i++) { product *= i; } System.out.println(product);
Answer
24
-
What will be the output of the following code?
int i = 5; while (i > 0) { i--; } System.out.println(i);
Answer
0
-
What will be the output of the following code?
int i = 1; do { i *= 2; } while (i < 10); System.out.println(i);
Answer
16
-
What will be the output of the following code?
int i = 1; for (; i <= 3; i++) { System.out.print(i + " "); }
Answer
1 2 3
-
What will be the output of the following code?
int count = 0; while (count < 2) { System.out.print(count + " "); count++; }
Answer
0 1
-
What will be the output of the following code?
int sum = 0; for (int i = 0; i < 4; i++) { sum += i; } System.out.println(sum);
Answer
6
-
What will be the output of the following code?
int i = 3; while (i <= 3) { i++; } System.out.println(i);
Answer
4
-
What will be the output of the following code?
int i = 1; do { i++; } while (i < 2); System.out.println(i);
Answer
2
-
What will be the output of the following code?
int i = 0; for (i = 1; i <= 5; i++) { if (i == 3) { continue; } System.out.print(i + " "); }
Answer
1 2 4 5
-
What will be the output of the following code?
int i = 0; do { i++; if (i == 2) { break; } } while (i < 5); System.out.println(i);
Answer
2
-
What will be the output of the following code?
int i = 1; for (i = 1; i <= 3; i++) { if (i % 2 == 0) { continue; } System.out.print(i + " "); }
Answer
1 3
-
What will be the output of the following code?
int i = 10; while (i > 5) { i -= 2; } System.out.println(i);
Answer
4
-
What will be the output of the following code?
int sum = 0; for (int i = 1; i <= 3; i++) { sum += i; } sum *= 2; System.out.println(sum);
Answer
12
-
What will be the output of the following code?
int i = 1; do { i += 3; } while (i < 5); System.out.println(i);
Answer
4
-
What will be the output of the following code?
int i = 2; while (i < 10) { i *= 2; } System.out.println(i);
Answer
16
-
What will be the output of the following code?
int i = 5; for (int j = 0; j < 3; j++) { i++; } System.out.println(i);
Answer
8
-
What will be the output of the following code?
int i = 0; while (i < 4) { i += 2; } System.out.println(i);
Answer
4
-
What will be the output of the following code?
int sum = 0; for (int i = 2; i <= 6; i += 2) { sum += i; } System.out.println(sum);
Answer
12
-
What will be the output of the following code?
int i = 1; do { i++; if (i == 3) { continue; } } while (i < 5); System.out.println(i);
Answer
5
-
What will be the output of the following code?
int i = 4; while (i > 1) { i--; } System.out.println(i);
Answer
1
-
What will be the output of the following code?
int i = 3; for (; i <= 6; i += 2) { System.out.print(i + " "); }
Answer
3 5
-
What will be the output of the following code?
int sum = 0; for (int i = 0; i < 3; i++) { sum += 2; } System.out.println(sum);
Answer
6
-
What will be the output of the following code?
int i = 2; do { i += 3; } while (i < 10); System.out.println(i);
Answer
11
-
What will be the output of the following code?
int sum = 1; for (int i = 1; i <= 3; i++) { sum *= i; } System.out.println(sum);
Answer
6
-
What will be the output of the following code?
int i = 1; while (i <= 3) { System.out.print(i + " "); i++; }
Answer
1 2 3
-
What will be the output of the following code?
int i = 2; do { System.out.print(i + " "); i++; } while (i < 4);
Answer
2 3
-
What will be the output of the following code?
int i = 0; for (; i < 3; i++) { System.out.print(i + " "); }
Answer
0 1 2
Tricky MCQs - Single Loop
-
What will be the output of the following code?
int i = 0; while (i < 5) { i++; if (i % 2 == 0) { i++; } System.out.print(i + " "); }
Answer
2 3 5 7
-
What will be the output of the following code?
int i = 1; do { i *= 2; if (i > 4) { break; } i++; } while (i < 8); System.out.println(i);
Answer
5
-
What will be the output of the following code?
int i = 10; for (int j = 0; j < 3; j++) { i -= j; } System.out.println(i);
Answer
7
-
What will be the output of the following code?
int i = 1; while (i <= 3) { System.out.print(i + " "); i++; if (i == 2) { i += 2; } }
Answer
1 4
-
What will be the output of the following code?
int i = 0; do { i++; if (i % 3 == 0) { continue; } System.out.print(i + " "); } while (i < 5);
Answer
1 2 4 5
-
What will be the output of the following code?
int i = 1; for (int j = 0; j < 5; j++) { if (j % 2 == 0) { i += j; } else { i -= j; } } System.out.println(i);
Answer
1
-
What will be the output of the following code?
int i = 1; while (i < 4) { i += i; System.out.print(i + " "); }
Answer
2 4 8
-
What will be the output of the following code?
int i = 2; do { i *= 2; } while (i < 16); System.out.println(i);
Answer
32
-
What will be the output of the following code?
int i = 1; for (int j = 5; j > 1; j--) { i += j; } System.out.println(i);
Answer
15
-
What will be the output of the following code?
int i = 1; while (i < 5) { if (i == 3) { break; } i++; } System.out.println(i);
Answer
3