Challenge - 5 Problems
Range Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple range iteration
What is the output of this Kotlin code that uses a range to iterate?
Kotlin
fun main() { for (i in 1..3) { print(i) } }
Attempts:
2 left
💡 Hint
Ranges in Kotlin include both start and end values when using '..'.
✗ Incorrect
The range 1..3 includes 1, 2, and 3. The loop prints each number without spaces, so output is '123'.
❓ Predict Output
intermediate2:00remaining
Range with step in iteration
What will this Kotlin code print when iterating over a range with a step?
Kotlin
fun main() { for (i in 1..6 step 2) { print(i) } }
Attempts:
2 left
💡 Hint
The step value skips numbers in the range by that amount.
✗ Incorrect
The range 1..6 with step 2 iterates over 1, 3, and 5. The print has no spaces, so output is '135'.
🔧 Debug
advanced2:00remaining
Why does this range iteration cause an error?
What error does this Kotlin code produce and why?
Kotlin
fun main() { for (i in 5..1) { print(i) } }
Attempts:
2 left
💡 Hint
Ranges with start greater than end are empty when using '..'.
✗ Incorrect
The range 5..1 is empty because the start is greater than the end. The loop does not run, so no output is printed.
❓ Predict Output
advanced2:00remaining
Output of a reversed range iteration
What is the output of this Kotlin code that iterates a reversed range?
Kotlin
fun main() { for (i in 5 downTo 1 step 2) { print(i) } }
Attempts:
2 left
💡 Hint
The 'downTo' keyword creates a decreasing range.
✗ Incorrect
The loop iterates from 5 down to 1, stepping by 2: 5, 3, 1. The print has no spaces, so output is '531'.
🧠 Conceptual
expert2:00remaining
Why do ranges simplify iteration in Kotlin?
Which statement best explains why ranges simplify iteration in Kotlin?
Attempts:
2 left
💡 Hint
Think about how ranges reduce the need for extra code in loops.
✗ Incorrect
Ranges let you write loops that automatically cover a sequence of values, so you don't need to write code to increase counters or check limits manually.