0
0
Kotlinprogramming~10 mins

Why ranges simplify iteration in Kotlin - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A0
B10
C5
D6
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.
2fill in blank
medium

Complete 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'
A1
B0
C10
D5
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.
3fill in blank
hard

Fix 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'
A5
B2
C1
D3
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.
4fill in blank
hard

Fill 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'
A1
B2
C10
D9
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.
5fill in blank
hard

Fill 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'
A10
B1
C3
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using step 2 or 1 which changes the numbers printed.
Swapping start and end values.