Challenge - 5 Problems
For Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a for loop over a list
What is the output of this Kotlin code?
Kotlin
val fruits = listOf("apple", "banana", "cherry") for (fruit in fruits) { print(fruit.first()) }
Attempts:
2 left
💡 Hint
Look at what the print statement outputs for each fruit.
✗ Incorrect
The loop prints the first character of each fruit name without spaces, so the output is 'abc'.
❓ Predict Output
intermediate2:00remaining
For loop with index over a list
What will be printed by this Kotlin code?
Kotlin
val numbers = listOf(10, 20, 30) for ((index, value) in numbers.withIndex()) { println("$index:$value") }
Attempts:
2 left
💡 Hint
withIndex() gives pairs of index and value.
✗ Incorrect
The loop prints each index and its corresponding value separated by a colon, each on a new line.
🔧 Debug
advanced2:00remaining
Identify the error in this for loop over a map
What error does this Kotlin code produce?
Kotlin
val map = mapOf("a" to 1, "b" to 2) for (key, value in map) { println("$key -> $value") }
Attempts:
2 left
💡 Hint
Check how to destructure map entries in Kotlin for loops.
✗ Incorrect
The for loop syntax for destructuring map entries requires parentheses around the pair: for ((key, value) in map). Missing parentheses cause a syntax error.
❓ Predict Output
advanced2:00remaining
Output of nested for loops over collections
What is the output of this Kotlin code?
Kotlin
val list1 = listOf(1, 2) val list2 = listOf("a", "b") for (num in list1) { for (char in list2) { print("$num$char ") } }
Attempts:
2 left
💡 Hint
The outer loop runs over numbers, inner over characters.
✗ Incorrect
The code prints each number combined with each character, separated by spaces, in order.
🧠 Conceptual
expert2:00remaining
Number of iterations in a for loop over a set
Given this Kotlin code, how many times will the loop run?
Kotlin
val set = setOf("x", "y", "z") var count = 0 for (item in set) { count++ } println(count)
Attempts:
2 left
💡 Hint
Count how many items are in the set.
✗ Incorrect
The set has 3 unique items, so the loop runs 3 times, incrementing count each time.