Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print numbers from 1 to 5 using a while loop.
Java
int i = 1; while (i [1] 5) { System.out.println(i); i++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i < 5' which stops before printing 5.
Using 'i > 5' which never runs the loop.
✗ Incorrect
The condition i <= 5 ensures the loop runs while i is 1 through 5.
2fill in blank
mediumComplete the code to stop the loop when the variable count reaches 10.
Java
int count = 0; while (count [1] 10) { System.out.println(count); count++; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' which causes the loop to never run.
Using '<=' which runs the loop one extra time.
✗ Incorrect
The loop should run while count is less than 10, so count < 10 is correct.
3fill in blank
hardFix the error in the while loop condition to avoid an infinite loop.
Java
int x = 5; while (x [1] 0) { System.out.println(x); x--; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes the loop to never run.
Using '>' alone excludes zero and may cause logic errors.
✗ Incorrect
The condition x >= 0 ensures the loop runs while x is 5 down to 0, avoiding infinite loop.
4fill in blank
hardFill both blanks to create a while loop that prints even numbers from 2 to 10.
Java
int num = 2; while (num [1] 10) { System.out.println(num); num [2] 2; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes 10 from output.
Using '-=' decreases num causing infinite loop.
✗ Incorrect
The condition num <= 10 includes 10, and num += 2 increases num by 2 each time.
5fill in blank
hardFill all three blanks to create a while loop that counts down from 15 to 5 by 2.
Java
int val = [1]; while (val [2] [3]) { System.out.println(val); val -= 2; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' causes the loop to never run.
Starting at 5 instead of 15 prints wrong numbers.
✗ Incorrect
Start at 15, loop while val is greater than or equal to 5, counting down by 2.