Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print numbers from 1 to 5 using a loop.
Java
for(int i = 1; i <= [1]; i++) { System.out.println(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 10 instead of 5 makes the loop run too many times.
Using 0 will not print any numbers because i starts at 1.
✗ Incorrect
The loop should run from 1 to 5, so the condition uses 5.
2fill in blank
mediumComplete the code to sum numbers from 1 to 4 using a loop.
Java
int sum = 0; for(int i = 1; i <= [1]; i++) { sum += i; } System.out.println(sum);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 will add an extra number.
Using 3 will miss the number 4.
✗ Incorrect
The loop should add numbers from 1 to 4, so the condition uses 4.
3fill in blank
hardFix the error in the loop condition to print numbers from 0 to 3.
Java
for(int i = 0; i [1] 3; i++) { System.out.println(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < will stop before printing 3.
Using == will only run once when i is 3.
✗ Incorrect
The loop should include 3, so use <= to include it.
4fill in blank
hardFill both blanks to create a loop that prints even numbers from 2 to 6.
Java
for(int i = [1]; i [2] 6; i += 2) { System.out.println(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 1 will print odd numbers.
Using < will miss printing 6.
✗ Incorrect
The loop starts at 2 and runs while i is less than or equal to 6 to include 6.
5fill in blank
hardFill all three blanks to create a loop that prints numbers from 5 down to 1.
Java
for(int i = [1]; i [2] 1; i [3]) { System.out.println(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ++ will increase i and cause an infinite loop.
Using <= will never run because 5 is not less than or equal to 1.
✗ Incorrect
The loop starts at 5, runs while i is greater than or equal to 1, and decreases i by 1 each time.