Challenge - 5 Problems
For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a basic for loop
What is the output of this Java code?
Java
public class Main { public static void main(String[] args) { for (int i = 0; i < 3; i++) { System.out.print(i + " "); } } }
Attempts:
2 left
💡 Hint
Remember the loop runs while i is less than 3, starting from 0.
✗ Incorrect
The loop starts at 0 and runs while i is less than 3, so it prints 0, 1, and 2 with spaces.
🧠 Conceptual
intermediate1:30remaining
Understanding for loop components
Which part of the for loop controls how many times the loop runs?
Attempts:
2 left
💡 Hint
Think about what decides if the loop continues or stops.
✗ Incorrect
The condition part is checked before each iteration; if false, the loop stops.
❓ Predict Output
advanced2:30remaining
Output with multiple variables in for loop
What is the output of this Java code?
Java
public class Main { public static void main(String[] args) { for (int i = 0, j = 3; i < 3; i++, j--) { System.out.print(i + "-" + j + " "); } } }
Attempts:
2 left
💡 Hint
Both i and j change each loop; watch their values carefully.
✗ Incorrect
The loop runs 3 times with i from 0 to 2 and j from 3 down to 1, printing pairs.
🔧 Debug
advanced2:00remaining
Identify the error in this for loop
What error does this Java code produce?
Java
public class Main { public static void main(String[] args) { for int i = 0; i < 5; i++) { System.out.print(i); } } }
Attempts:
2 left
💡 Hint
Check the syntax of the for loop header carefully.
✗ Incorrect
The for loop header must be enclosed in parentheses. Missing them causes a syntax error.
🚀 Application
expert3:00remaining
Number of iterations in nested for loops
How many times does the inner loop run in total when this code executes?
Java
public class Main { public static void main(String[] args) { int count = 0; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { count++; } } System.out.println(count); } }
Attempts:
2 left
💡 Hint
Count how many times the inner loop runs for each i from 1 to 4.
✗ Incorrect
The inner loop runs 1 + 2 + 3 + 4 = 10 times in total.