Challenge - 5 Problems
Kotlin For Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple for loop with range
What is the output of this Kotlin code?
Kotlin
fun main() { for (i in 1..3) { print(i) } }
Attempts:
2 left
💡 Hint
Remember that the range 1..3 includes both 1 and 3, and print() does not add spaces or newlines.
✗ Incorrect
The for loop iterates over 1, 2, and 3. The print function outputs each number without spaces or newlines, so the output is '123'.
❓ Predict Output
intermediate2:00remaining
For loop with step in range
What is the output of this Kotlin code?
Kotlin
fun main() { for (i in 0..6 step 2) { print(i) } }
Attempts:
2 left
💡 Hint
The step keyword skips numbers by the given step size.
✗ Incorrect
The loop starts at 0 and goes up to 6, stepping by 2 each time, so it prints 0, 2, 4, 6 without spaces.
❓ Predict Output
advanced2:00remaining
For loop with downTo and step
What is the output of this Kotlin code?
Kotlin
fun main() { for (i in 10 downTo 2 step 3) { print(i) } }
Attempts:
2 left
💡 Hint
downTo counts backwards, step skips numbers by the step size.
✗ Incorrect
The loop starts at 10, then 7, then 4, then stops because next step 1 is less than 2. So output is '1074'.
🔧 Debug
advanced2:00remaining
Identify the error in the for loop with range
What error does this Kotlin code produce?
Kotlin
fun main() { for (i in 1..5 step) { print(i) } }
Attempts:
2 left
💡 Hint
Check the syntax of the step keyword in the range expression.
✗ Incorrect
The 'step' keyword requires a number after it. Missing the number causes a syntax error.
🧠 Conceptual
expert2:00remaining
Number of iterations in a for loop with range and step
How many times does this Kotlin for loop run?
Kotlin
fun main() { var count = 0 for (i in 5 downTo 0 step 2) { count++ } println(count) }
Attempts:
2 left
💡 Hint
List the numbers generated by the range and count them.
✗ Incorrect
The loop runs for i = 5, 3, 1. It stops before reaching -1. So it runs 3 times.