Bird
Raised Fist0
Javaprogramming~20 mins

Loop initialization, condition, update in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a for-loop with multiple updates
What is the output of the following Java code?
Java
public class Main {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1, j = 10; i < j; i += 2, j -= 3) {
            sum += i + j;
        }
        System.out.println(sum);
    }
}
A21
B50
C45
D55
Attempts:
2 left
💡 Hint
Trace the values of i and j in each loop iteration and add their sum.
❓ Predict Output
intermediate
2:00remaining
Output of a while-loop with complex condition
What is the output of this Java code snippet?
Java
public class Main {
    public static void main(String[] args) {
        int i = 0;
        int count = 0;
        while (i < 10) {
            i += 3;
            if (i % 2 == 0) {
                count++;
            }
        }
        System.out.println(count);
    }
}
A5
B3
C4
D2
Attempts:
2 left
💡 Hint
Check the values of i after each increment and count how many are even.
🔧 Debug
advanced
2:00remaining
Identify the error in loop initialization
What error will this Java code produce when compiled?
Java
public class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++)
            int j = i * 2;
        System.out.println(j);
    }
}
ACompilation error: variable j might not have been initialized
BCompilation error: cannot find symbol j
CRuntime error: NullPointerException
DOutput: 8
Attempts:
2 left
💡 Hint
Check the scope of variable j declared inside the loop.
❓ Predict Output
advanced
2:00remaining
Output of nested for-loops with update expressions
What is the output of this Java program?
Java
public class Main {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 1; i <= 3; i++) {
            for (int j = i; j <= 3; j += 2) {
                count++;
            }
        }
        System.out.println(count);
    }
}
A7
B6
C4
D8
Attempts:
2 left
💡 Hint
Count how many times the inner loop runs for each i.
🧠 Conceptual
expert
2:00remaining
Effect of loop update expression on iteration count
Consider the loop:
for (int i = 0; i < 20; i += 4) { /* body */ }

How many times will the loop body execute?
A5
B4
C6
D7
Attempts:
2 left
💡 Hint
Count how many increments of 4 fit before i reaches 20.

Practice

(1/5)
1. Which part of a for loop in Java sets the starting value of the loop variable?
easy
A. Initialization
B. Condition
C. Update
D. Body

Solution

  1. Step 1: Understand the parts of a for loop

    A for loop has three parts: initialization, condition, and update.
  2. Step 2: Identify the starting value part

    The initialization part sets the starting value of the loop variable before the loop runs.
  3. Final Answer:

    Initialization -> Option A
  4. Quick Check:

    Starting value = Initialization [OK]
Hint: Initialization always sets the loop's start value [OK]
Common Mistakes:
  • Confusing condition with initialization
  • Thinking update sets the start value
  • Mixing loop body with initialization
2. Which of the following is the correct syntax for a for loop in Java that counts from 0 to 4?
easy
A. for(int i = 0; i < 4; i++) {}
B. for(int i = 0; i <= 4; i++) {}
C. for(int i = 1; i <= 5; i++) {}
D. for(int i = 0; i != 5; i--) {}

Solution

  1. Step 1: Check initialization and condition

    for(int i = 0; i <= 4; i++) {} starts at 0 and runs while i is less than or equal to 4, which covers 0 to 4.
  2. Step 2: Check update direction

    for(int i = 0; i <= 4; i++) {} increments i by 1 each time, moving forward correctly.
  3. Final Answer:

    for(int i = 0; i <= 4; i++) {} -> Option B
  4. Quick Check:

    0 to 4 inclusive = for(int i = 0; i <= 4; i++) {} [OK]
Hint: Use i <= max to include last number [OK]
Common Mistakes:
  • Using i < 4 excludes 4
  • Starting at 1 misses 0
  • Decrementing i causes infinite loop
3. What is the output of this Java code?
for(int i = 1; i < 5; i += 2) {
    System.out.print(i + " ");
}
medium
A. 1 2 3 4
B. 1 3 5
C. 2 4
D. 1 3

Solution

  1. Step 1: Trace loop values

    i starts at 1, prints 1, then i increases by 2 to 3, prints 3, then i increases by 2 to 5.
  2. Step 2: Check loop condition

    Loop runs while i < 5. When i is 5, condition fails, so loop stops.
  3. Final Answer:

    1 3 -> Option D
  4. Quick Check:

    Prints 1 and 3 only [OK]
Hint: Add update to i and check condition carefully [OK]
Common Mistakes:
  • Assuming loop runs when i equals 5
  • Forgetting i increments by 2
  • Printing values after loop ends
4. Identify the error in this Java loop:
for(int i = 0; i > 5; i++) {
    System.out.println(i);
}
medium
A. Condition causes loop to never run
B. Update should decrement i
C. Initialization is incorrect
D. Missing loop body braces

Solution

  1. Step 1: Analyze the loop condition

    The condition is i > 5, but i starts at 0, so 0 > 5 is false immediately.
  2. Step 2: Understand loop execution

    Since condition is false at start, loop body never runs.
  3. Final Answer:

    Condition causes loop to never run -> Option A
  4. Quick Check:

    False condition stops loop immediately [OK]
Hint: Check if condition is true at start to run loop [OK]
Common Mistakes:
  • Thinking loop runs at least once
  • Confusing > with < in condition
  • Assuming update fixes condition
5. You want to print all even numbers from 2 to 20 using a for loop in Java. Which loop correctly does this?
hard
A. for(int i = 2; i < 20; i++) { if(i % 2 == 0) System.out.print(i + " "); }
B. for(int i = 1; i < 20; i++) { if(i % 2 == 0) System.out.print(i + " "); }
C. for(int i = 2; i <= 20; i += 2) { System.out.print(i + " "); }
D. for(int i = 0; i <= 20; i += 2) { System.out.print(i + " "); }

Solution

  1. Step 1: Check initialization and update for even numbers

    for(int i = 2; i <= 20; i += 2) { System.out.print(i + " "); } starts at 2 and increments by 2, printing even numbers directly up to 20.
  2. Step 2: Verify condition includes 20

    Condition i <= 20 ensures 20 is included in output.
  3. Final Answer:

    for(int i = 2; i <= 20; i += 2) { System.out.print(i + " "); } -> Option C
  4. Quick Check:

    Start at 2, step 2, up to 20 [OK]
Hint: Start at first even, add 2 each time [OK]
Common Mistakes:
  • Starting at 0 prints unwanted 0
  • Using < 20 excludes 20
  • Forgetting to increment by 2
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