Java Loops Quiz: 35 MCQ Questions for Practice

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?

Which loop in Java is guaranteed to execute its body at least once?
A. for loop
B. while loop
C. do-while loop
D. enhanced for loop
do-while loop
The do-while loop checks its condition after executing the body, so the body always runs at least once, even if the condition is false from the start.

What is the output of the following code?

for (int i = 0; i < 5; i++) {
    System.out.print(i + " ");
}
A. 0 1 2 3 4
B. 1 2 3 4 5
C. 0 1 2 3 4 5
D. 1 2 3 4
0 1 2 3 4
  • 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.
Which keyword is used to skip the current iteration and move to the next in a loop?
A. break
B. skip
C. continue
D. return
continue
The continue statement skips the rest of the current iteration and jumps to the loop’s update expression (for-loop) or re-evaluates the condition (while/do-while).

What is the output of the following code?

int i = 1;
while (i <= 5) {
    System.out.print(i + " ");
    i += 2;
}
A. 1 2 3 4 5
B. 1 3 5
C. 1 3 5 7
D. 2 4
1 3 5
  • 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);
A. 10
B. No output
C. 10 9 8…
D. Compilation Error
10
  • 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.
Which loop is best suited when the number of iterations is known in advance?
A. while loop
B. do-while loop
C. for loop
D. enhanced for loop
for loop
The for loop bundles initialization, condition, and update in a single line, making it the most readable and idiomatic choice when the iteration count is known beforehand.
What is the correct syntax for an enhanced for loop (for-each) in Java?
A. for (int i : array) { }
B. for (array : int i) { }
C. foreach (int i in array) { }
D. for each (int i : array) { }
for (int i : array) { }
  • 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 + " ");
    }
}
A. 1 2 2 4
B. 1 2 3 4
C. 2 4 4 8
D. 1 4 2 4
1 2 2 4
  • 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.
What keyword immediately terminates a loop in Java?
A. stop
B. exit
C. continue
D. break
break
  • 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 + " ");
}
A. 0 1 2
B. 0 2
C. 1
D. 0 1
0 2
  • 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. Accessible everywhere in the method
B. Only inside the for loop block
C. Accessible after the loop ends
D. Depends on the access modifier
Only inside the for loop block
  • 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.”
Which of the following creates an infinite loop?
A. for (int i = 0; i < 10; i++)
B. while (false) { }
C. for (;;) { }
D. do { } while (false);
for (;;) { }
  • 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 + " ");
    }
}
A. 00 01 10 11 20 21
B. 00
C. 00 10 20
D. Compilation Error
00
  • 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]);
A. 10
B. 1
C. 30
D. Compilation Error
1
  • 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 + " ");
}
A. 100
B. 50
C. 7
D. 10
7
  • 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);
A. 55
B. 25
C. 30
D. 50
25
  • 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);
A. 3
B. 5
C. 7
D. 9
3
  • 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 + " ");
}
A. 5 4 3 2 1
B. 4 3 2 1 0
C. 5 4 3 2 1 0
D. 4 3 2 1
4 3 2 1 0
  • 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. 010 19 28 37 46
B. 010 19 28
C. Compilation Error
D. 010 19 28 37
010 19 28 37 46
  • 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.
Which statement about the enhanced for loop is false?
A. It can iterate over arrays
B. It can iterate over Collections
C. You can modify the original array elements via the loop variable
D. It cannot be used to iterate in reverse order
You can modify the original array elements via the loop variable
  • 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");
    }
}
A. 16
B. 10
C. 12
D. 8
10
  • 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");
}
A. AAAAAAAAAA (10 times)
B. A (once)
C. No output
D. Infinite loop printing A forever
Infinite loop printing A forever
  • 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);
A. 5
B. 0
C. 4
D. Compilation Error
0
  • 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;
    }
}
A. 0 1 2 3 4
B. 0 1 2 3 2 3 2 3 … (infinite)
C. 0 1 2 3 2 3 1 2 3 … (infinite)
D. 0 1 2 3
0 1 2 3 2 3 2 3 … (infinite)
  • 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);
}
A. 555
B. 012
C. Compilation Error
D. Runtime Error
Compilation Error
  • 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);
A. 2
B. 3
C. 4
D. 5
3
  • 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 + " ");
    }
}
A. 00 01 02 10 12 20 21 22
B. 00 01 02 10 20 21 22
C. 00 01 02 10 11 12 20 21 22
D. 00 01 02 10 12 21 22
00 01 02 10 20 21 22
  • 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 + " ");
}
A. 1 2 4 8 16
B. 2 4 8 16 32
C. 1 3 7 15
D. 1 2 3 4
1 2 4 8 16
  • 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;
}
A. 9
B. 8
C. -1
D. 6
8
  • 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
    }
}
A. O(n²)
B. O(n log n)
C. O(log n)
D. O(n)
O(n log n)
  • 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. 9
B. 6
C. 3
D. 0
6
  • 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;
    }
}
A. 1024
B. 512
C. 256
D. 128
1024
  • 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);
A. 4
B. 5
C. 8
D. 25
4
  • 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
        }
    }
}
A. n(n+1)/2
B. n(n+1)(n+2)/6
C. n²(n+1)/2
D. n³/6
n(n+1)(n+2)/6
  • 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);
A. 10 6
B. 6 3
C. 10 3
D. 6 6
6 3
  • 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.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.