Java loops are one of the most important concepts in Java programming because they help execute a block of code repeatedly and efficiently. Understanding loops is essential for writing optimized programs, solving coding problems, and building real-world Java applications.
This Java Loops quiz contains the top 35 multiple-choice questions with answers and explanations. We have covered the topics for, while, do-while, nested loops, infinite loops, loop control statements, and advanced looping concepts in Java.
Whether you are students, beginners, or programmers, this quiz will help you to test your knowledge on Java loops. By practicing these MCQs, you can improve your logical thinking, coding skills, and preparation for interviews, exams, and competitive programming.
There is no time limit to solve questions. At the end of the quiz, you will get the scorecard, which will show the performance in the quiz.
Java Loops MCQ Quiz
Let’s start the MCQ quiz and check how well you know loops in Java programming! Can you score 35/35?
What is the output of the following code?
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
}- The loop starts at i=0 and runs while i<5.
- It prints 0, 1, 2, 3, 4 — not 5, since 5 fails the condition.
- Output: 0 1 2 3 4.
What is the output of the following code?
int i = 1;
while (i <= 5) {
System.out.print(i + " ");
i += 2;
}- Starting at i=1, the value increments by 2 each iteration: 1 → 3 → 5 → 7.
- 7 fails the condition i<=5, so the loop stops. Output: 1 3 5.
What will be the output of this do-while loop?
int x = 10;
do {
System.out.print(x + " ");
x--;
} while (x > 12);- The body executes once before checking the condition.
- x=10 is printed, then x becomes 9.
- Condition 9 > 12 is false, so the loop ends. Output: 10.
- Java’s enhanced for-each syntax is: for (Type var : iterable) { }.
- It iterates over each element of an array or Collection automatically.
- Options C and D use syntax from C# and other languages, not Java.
What is the output of the following nested loop?
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 2; j++) {
System.out.print(i * j + " ");
}
}- i=1: j=1 → 1*1=1, j=2 → 1*2=2.
- i=2: j=1 → 2*1=2, j=2 → 2*2=4.
- Output: 1 2 2 4.
- The break statement immediately exits the innermost enclosing loop (or switch statement).
- Execution continues with the statement right after the loop body.
- The continue statement only skips the current iteration, not the entire loop.
What will this code print?
for (int i = 0; i < 3; i++) {
if (i == 1) continue;
System.out.print(i + " ");
}- When i==1, continue skips the print statement for that iteration.
- So 0 is printed, 1 is skipped, and 2 is printed.
- Output: 0 2.
What is the scope of the loop variable declared in a for loop?
for (int i = 0; i < 5; i++) { }
System.out.println(i); // ?- A variable declared in a for loop’s initialization section (e.g., int i=0) is scoped to the loop block only.
- Trying to access i outside the loop causes a compile-time error: “cannot find symbol.”
- The for(;;) loop has no initialization, no condition (defaults to true), and no update — making it an infinite loop.
- It is a common idiom in Java for intentional infinite loops.
- Option B never executes at all. Option D executes only once.
What is the output of this labeled break?
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (j == 1) break outer;
System.out.print(i + "" + j + " ");
}
}- In the very first outer iteration (i=0), when j==1, break outer exits both loops immediately.
- Only i=0, j=0 is printed before the break fires.
- Output: 00.
What is the result of modifying an array element inside an enhanced for loop?
int[] arr = {1, 2, 3};
for (int x : arr) {
x = x * 10;
}
System.out.print(arr[0]);- x is a copy of the array element, not a reference to it.
- Modifying x does not affect the original array, so arr[0] remains 1.
- For primitive arrays, the for-each loop cannot be used to modify elements in-place.
How many times does this loop execute?
for (int i = 1; i <= 100; i *= 2) {
System.out.print(i + " ");
}- The variable i doubles each iteration: 1 → 2 → 4 → 8 → 16 → 32 → 64 → 128 (128 > 100, loop stops).
- Values printed: 1, 2, 4, 8, 16, 32, 64 — exactly 7 iterations.
- This is O(log n) behavior — doubling produces logarithmic iteration counts.
What is the output of the following code?
int sum = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) continue;
sum += i;
}
System.out.println(sum);- The continue statement skips even numbers entirely.
- Only odd numbers (1, 3, 5, 7, 9) are added: 1 + 3 + 5 + 7 + 9 = 25.
What is the output?
int x = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 != 0) {
x += i;
} else {
x -= i;
}
}
System.out.println(x);- i=1 (odd): x = 0+1 = 1.
- i=2 (even): x = 1-2 = -1.
- i=3 (odd): x = -1+3 = 2.
- i=4 (even): x = 2-4 = -2.
- i=5 (odd): x = -2+5 = 3. Output: 3.
What is the output?
int i = 5;
while (i-- > 0) {
System.out.print(i + " ");
}- i– is post-decrement: the condition is checked with the current value, then i is decremented.
- Check i=5 (true) → i becomes 4 → print 4. Check i=4 (true) → i becomes 3 → print 3. And so on.
- Check i=1 (true) → i becomes 0 → print 0. Check i=0 (false) → stop.
- Output: 4 3 2 1 0.
What will this for loop print?
for (int i = 0, j = 10; i < j; i++, j--) {
System.out.print(i + "" + j + " ");
}- A for loop can declare and update multiple variables using commas.
- i=0,j=10 → “010”; i=1,j=9 → “19”; i=2,j=8 → “28”; i=3,j=7 → “37”; i=4,j=6 → “46”.
- i=5,j=5 → 5<5 is false → stop. Output: 010 19 28 37 46.
- For primitive arrays, the loop variable is a copy — modifying it does not affect the original array.
- For objects, you can call mutating methods on the reference, but reassigning the variable still does not change the array.
- Options A, B, and D are all true statements.
How many times is ‘X’ printed?
for (int i = 0; i < 4; i++) {
for (int j = i; j < 4; j++) {
System.out.print("X");
}
}- i=0: j runs from 0 to 3 → 4 times.
- i=1: j runs from 1 to 3 → 3 times.
- i=2: j runs from 2 to 3 → 2 times.
- i=3: j runs from 3 to 3 → 1 time. Total: 4+3+2+1 = 10.
What is the output of this tricky loop?
for (int i = 0; i < 10; i = i++) {
System.out.print("A");
}- i = i++ is the update step. i++ returns the old value of i (0) and schedules i to become 1.
- But then the old value (0) is assigned back to i — so i stays 0 forever.
- In Java, post-increment is well-defined: the assignment wins and i resets to 0 every iteration, causing an infinite loop.
What is the value of x after this loop?
int x = 0;
for (int i = 0; i < 5; i++) {
x = x++;
}
System.out.println(x);- x = x++: post-increment returns the old value of x (0 initially), increments x to 1, but then assigns the old value (0) back to x.
- This repeats every iteration — x stays 0 throughout all 5 iterations.
- Output: 0.
What is the output of the following code snippet?
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
if (i == 3) {
i = 1;
}
}- When i=3 is printed, i is reset to 1.
- Then the for-loop update does i++, making i=2.
- Next iteration prints 2, then 3, resets to 1, increments to 2 again — the sequence 2 3 repeats forever. Infinite Loop.
What is the output of this code?
for (int i = 0; i < 3; i++) {
int i = 5;
System.out.print(i);
}- Redeclaring a variable i inside the for block when i is already in scope (from the for-init section) causes a compile-time error.
- The error is: “Variable ‘i’ is already defined in the scope.”
- Java does not allow shadowing of variables within the same scope.
What does this code print?
int count = 0;
for (int i = 0; i < 10; i++) {
if (i % 3 == 0) continue;
if (i % 5 == 0) break;
count++;
}
System.out.println(count);- i=0: 0%3==0 → continue (skip). i=1: count=1. i=2: count=2.
- i=3: 3%3==0 → continue (skip). i=4: count=3.
- i=5: 5%5==0 → break. Final count = 3.
What is the output?
outer:
for (int i = 0; i < 3; i++) {
inner:
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) continue outer;
System.out.print(i + "" + j + " ");
}
}- When i=1 and j=1: the continue outer skips all remaining j iterations for i=1.
- So i=1 only prints j=0 (i.e., “10”), then skips j=1 and j=2.
- i=0 prints all (00, 01, 02), i=2 prints all (20, 21, 22). Output: 00 01 02 10 20 21 22.
What is the output of this loop with bitwise operations?
for (int i = 1; i <= 16; i <<= 1) {
System.out.print(i + " ");
}- i <<= 1 is a left-shift by 1 bit, equivalent to multiplying by 2.
- i progression: 1 → 2 → 4 → 8 → 16 → 32 (32 fails <=16 → stop).
- Output: 1 2 4 8 16.
What does the find() method return?
static int find() {
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
if (i + j == 6) return i * j;
return -1;
}- The loops search in row-major order for the first pair where i + j == 6.
- i=2, j=4 → 2+4=6 → return 2*4 = 8. (i=3, j=3 satisfies the condition too, but i=2, j=4 is found first.)
What is the time complexity of this loop?
for (int i = n; i > 0; i /= 2) {
for (int j = 0; j < n; j++) {
// O(1) work
}
}- Outer loop: i halves each iteration → runs O(log n) times.
- Inner loop: runs O(n) times for each outer iteration.
- Total: O(n) × O(log n) = O(n log n) — the same complexity as merge sort and heap sort.
What is the output of the following program?
public class Test {
public static void main(String[] args) {
int i, j, k = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (i == j) continue;
k++;
}
}
System.out.println(k);
}
}- A 3×3 nested loop gives 9 total (i,j) pairs.
- continue fires when i==j (the diagonal): (0,0), (1,1), (2,2) — 3 pairs skipped.
- k is incremented only when i≠j: 9 – 3 = 6. Output: 6.
What is the value of x after execution?
int x = 1;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
x *= 2;
}
}- x is multiplied by 2 for each (i, j) pair. Total multiplications = sum of inner loop counts.
- i=1: 1 time, i=2: 2 times, i=3: 3 times, i=4: 4 times. Total = 1+2+3+4 = 10.
- x = 1 × 2^10 = 1024.
What does this program print?
int n = 100, count = 0;
for (int i = 2; i * i <= n; i++) {
boolean prime = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) { prime = false; break; }
}
if (prime) count++;
}
System.out.println(count);- The outer loop runs while i*i <= 100, meaning i goes from 2 to 10.
- Primes in the range [2, 10]: 2, 3, 5, 7 → exactly 4 primes.
- Note: this does not count all primes up to 100 — it only counts primes up to sqrt(100) = 10.
How many times is statement S executed?
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
for (int k = 1; k <= j; k++) {
S; // some O(1) statement
}
}
}- This triple nested loop counts triplets (i,j,k) with 1 ≤ k ≤ j ≤ i ≤ n.
- This is equivalent to C(n+2, 3) = (n+2)(n+1)n / 6 = n(n+1)(n+2)/6 — the formula for tetrahedral numbers.
- Example: n=3 → 3×4×5/6 = 10. Verified manually: i=1→1, i=2→3, i=3→6. Total = 1+3+6 = 10.
What is the value of ‘a’ and ‘b’ after execution?
int a = 0, b = 0;
for (int i = 0; i < 5; i++) {
a += i;
if (i == 3) break;
b += i;
}
System.out.println(a + " " + b);- i=0: a=0, b=0. i=1: a=1, b=1. i=2: a=3, b=3.
- i=3: a=3+3=6, then break fires — b is NOT updated for i=3.
- Final: a=6, b=3. KEY INSIGHT: a is updated before the break check, but b comes after — so b misses the i=3 contribution.






