Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the loop variable correctly.
Java
for ([1]; i < 5; i++) { System.out.println(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to declare the variable type.
Using assignment without declaration.
✗ Incorrect
The loop variable must be declared and initialized as int i = 0 inside the for loop.
2fill in blank
mediumComplete the code to write the correct loop condition.
Java
for (int i = 0; [1]; i++) { System.out.println(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= 5 runs the loop one extra time.
Using == 5 does not run the loop at all.
✗ Incorrect
The loop should run while i is less than 5, so the condition is i < 5.
3fill in blank
hardFix the error in the loop update expression.
Java
for (int i = 0; i < 5; [1]) { System.out.println(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using decrement
i-- causes an infinite loop.Using
i += 2 skips numbers.✗ Incorrect
The update expression i++ increases i by 1 each loop, which is the standard increment.
4fill in blank
hardFill both blanks to create a loop that counts down from 10 to 1.
Java
for (int i = [1]; i [2] 0; i--) { System.out.println(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 instead of 10.
Using
i < 0 as condition causes no iterations.✗ Incorrect
The loop starts at 10 and runs while i > 0, printing numbers from 10 down to 1.
5fill in blank
hardFill all three blanks to create a loop that prints even numbers from 2 to 10.
Java
for (int [1] = [2]; [1] [3] 10; [1] += 2) { System.out.println([1]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names.
Starting at 0 or 1 instead of 2.
Using
< instead of <= causes missing 10.✗ Incorrect
The loop variable is i, starting at 2, running while i <= 10, and increasing by 2 each time to print even numbers.