Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print numbers from 1 to 5 using a range.
Kotlin
for (i in 1..[1]) { println(i) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number outside the desired range like 6 or 10.
Using 0 which is less than the start of the range.
✗ Incorrect
The range 1..5 includes numbers from 1 to 5, so the loop prints these numbers.
2fill in blank
mediumComplete the code to print numbers from 5 down to 1 using a range.
Kotlin
for (i in [1] downTo 1) { println(i) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 before
downTo which would create an empty range.Using 0 or 10 which are outside the desired range.
✗ Incorrect
The downTo function creates a decreasing range from 5 down to 1.
3fill in blank
hardFix the error in the code to print even numbers from 2 to 10 using a range with step.
Kotlin
for (i in 2..10 step [1]) { println(i) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using step 1 which prints all numbers, not just even ones.
Using step 3 or 5 which skips some even numbers.
✗ Incorrect
Using step 2 makes the loop count by twos, printing even numbers.
4fill in blank
hardFill both blanks to create a range that prints numbers from 1 to 10 but only odd numbers.
Kotlin
for (i in [1]..[2] step 2) { println(i) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 2 which would print even numbers.
Ending at 10 which includes an even number and would print 10 if stepped by 2.
✗ Incorrect
Starting at 1 and stepping by 2 up to 9 prints odd numbers: 1, 3, 5, 7, 9.
5fill in blank
hardFill all three blanks to create a range that prints numbers from 10 down to 1, stepping by 3.
Kotlin
for (i in [1] downTo [2] step [3]) { println(i) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using step 2 or 1 which changes the numbers printed.
Swapping start and end values.
✗ Incorrect
This loop counts down from 10 to 1, stepping by 3, printing 10, 7, 4, 1.