Bird
Raised Fist0
Javaprogramming~20 mins

Nested for loop in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Nested Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested for loop with multiplication table
What is the output of the following Java code?
Java
public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 2; i++) {
            for (int j = 1; j <= 3; j++) {
                System.out.print(i * j + " ");
            }
            System.out.println();
        }
    }
}
A
1 2 3 
2 4 6 
B
1 2 3 
1 2 3 
C
1 1 1 
2 2 2 
D
1 3 6 
2 5 9 
Attempts:
2 left
💡 Hint
Think about how the outer and inner loops multiply their counters.
❓ Predict Output
intermediate
1:30remaining
Number of iterations in nested loops
How many times will the innermost statement execute in this code?
Java
for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 5; j++) {
        System.out.println(i + "," + j);
    }
}
A10
B25
C20
D9
Attempts:
2 left
💡 Hint
Multiply the number of iterations of outer and inner loops.
🔧 Debug
advanced
2:00remaining
Identify the error in nested for loop
What error will this code produce when compiled?
Java
for (int i = 0; i < 3; i++)
    for (int j = 0; j < 2; j++)
        System.out.println(i + j);
}
ASyntaxError: missing semicolon
BSyntaxError: unexpected '}'
CNo error, runs fine
DRuntimeException: NullPointerException
Attempts:
2 left
💡 Hint
Check the braces and semicolons carefully.
❓ Predict Output
advanced
2:00remaining
Output of nested loops with break statement
What is the output of this Java code?
Java
public class Main {
    public static void main(String[] args) {
        for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                if (j == 2) break;
                System.out.print(i * j + " ");
            }
            System.out.println();
        }
    }
}
A
1 
2 
3 
B
1 2 
2 4 
3 6 
C
1 2 3 
1 2 3 
1 2 3 
D
1 
2 
3 
4 
Attempts:
2 left
💡 Hint
The break stops the inner loop when j equals 2.
🧠 Conceptual
expert
2:30remaining
Effect of modifying loop variable inside nested loop
Consider this code snippet. What will be the value of i after the loops finish?
Java
int i = 0;
for (; i < 3; i++) {
    for (int j = 0; j < 2; j++) {
        if (j == 1) {
            i++;
        }
    }
}
System.out.println(i);
A5
B3
C6
D4
Attempts:
2 left
💡 Hint
Remember i is incremented both by the for loop and inside the inner loop.

Practice

(1/5)
1. What does a nested for loop in Java do?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
// code
}
}
easy
A. Runs one loop inside another to repeat code multiple times
B. Runs two loops one after another sequentially
C. Runs only the inner loop multiple times
D. Runs only the outer loop once

Solution

  1. Step 1: Analyze nested loop iterations

    The outer loop runs 3 times, and for each outer loop iteration, the inner loop runs 2 times.
  2. Step 2: Explain the repetition behavior

    This means the inner loop runs fully inside each outer loop iteration, repeating code multiple times in total.
  3. Final Answer:

    Runs one loop inside another to repeat code multiple times -> Option A
  4. Quick Check:

    Nested loops = inner loop inside outer loop [OK]
Hint: Nested loops repeat inner loop fully for each outer loop [OK]
Common Mistakes:
  • Thinking loops run sequentially, not nested
  • Confusing inner and outer loop counts
  • Assuming only one loop runs multiple times
2. Which of the following is the correct syntax for a nested for loop in Java?
easy
A. for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) System.out.println(i + j); }
B. for int i = 0; i < 3; i++ { for int j = 0; j < 2; j++ { System.out.println(i + j); } }
C. for (int i = 0; i < 3; i++) for (int j = 0; j < 2; j++ { System.out.println(i + j); }
D. for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) System.out.println(i + j); } else {}

Solution

  1. Step 1: Verify Java for loop syntax requirements

    Java for loops require parentheses around initialization, condition, and increment, and braces for blocks.
  2. Step 2: Identify correct nested loop syntax

    for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) System.out.println(i + j); } correctly uses braces for outer loop without extra invalid keywords or else blocks.
  3. Final Answer:

    for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) System.out.println(i + j); } -> Option A
  4. Quick Check:

    Correct Java syntax = for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) System.out.println(i + j); } [OK]
Hint: Remember Java for loop needs parentheses and braces [OK]
Common Mistakes:
  • Missing parentheses around for loop conditions
  • Using else after for loop incorrectly
  • Omitting braces for multiple statements
3. What is the output of this Java code?
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print(i * j + " ");
}
System.out.println();
}
medium
A. 2 4 6 4 8 12
B. 1 2 3 4 5 6
C. 1 2 3 1 2 3
D. 1 2 3 2 4 6

Solution

  1. Step 1: Trace outer loop iterations

    Outer loop runs i=1 and i=2.
  2. Step 2: Calculate inner loop outputs for each i

    For i=1: j=1,2,3 outputs 1*1=1, 1*2=2, 1*3=3.
    For i=2: j=1,2,3 outputs 2*1=2, 2*2=4, 2*3=6.
  3. Final Answer:

    1 2 3 2 4 6 -> Option D
  4. Quick Check:

    Multiply i and j in nested loops = 1 2 3 2 4 6 [OK]
Hint: Multiply outer and inner loop variables for output [OK]
Common Mistakes:
  • Ignoring new line after inner loop
  • Concatenating all numbers in one line
  • Mixing up i and j values
4. Find the error in this nested for loop code:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
System.out.println(i + j);
}
}
medium
A. Missing semicolon after inner loop
B. Inner loop condition should be j <= 3
C. Extra closing brace '}' after inner loop
D. Outer loop variable i is not initialized

Solution

  1. Step 1: Check braces matching

    The inner loop lacks braces but has an extra closing brace '}' after it, causing syntax error.
  2. Step 2: Identify correct brace placement

    Remove the extra '}' after inner loop or add braces properly to fix syntax.
  3. Final Answer:

    Extra closing brace '}' after inner loop -> Option C
  4. Quick Check:

    Brace mismatch error = Extra closing brace '}' after inner loop [OK]
Hint: Match opening and closing braces carefully [OK]
Common Mistakes:
  • Adding extra braces after loops
  • Omitting braces for multiple statements
  • Incorrect loop conditions
5. How can you use nested for loops to print a 4x4 multiplication table in Java?
hard
A. Use a single loop from 1 to 16 and print i * i
B. Use outer loop for rows 1 to 4 and inner loop for columns 1 to 4, print product
C. Use nested loops but print only the outer loop variable
D. Use nested loops but skip printing inside inner loop

Solution

  1. Step 1: Set outer loop for rows 1 to 4

    The outer loop controls the row number from 1 to 4.
  2. Step 2: Set inner loop for columns 1 to 4 and print product

    The inner loop runs columns 1 to 4, printing the product of row and column numbers to form the table.
  3. Final Answer:

    Use outer loop for rows 1 to 4 and inner loop for columns 1 to 4, print product -> Option B
  4. Quick Check:

    Nested loops for rows and columns = Use outer loop for rows 1 to 4 and inner loop for columns 1 to 4, print product [OK]
Hint: Outer loop rows, inner loop columns, print product [OK]
Common Mistakes:
  • Using single loop for 2D table
  • Not printing product of both loop variables
  • Skipping inner loop printing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard