Challenge - 5 Problems
It Keyword Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of lambda using 'it' keyword
What is the output of this Kotlin code snippet using the 'it' keyword in a lambda?
Kotlin
val numbers = listOf(1, 2, 3, 4) val doubled = numbers.map { it * 2 } println(doubled)
Attempts:
2 left
💡 Hint
The 'it' keyword refers to the single parameter of the lambda function.
✗ Incorrect
The 'map' function applies the lambda to each element. 'it' is each element, so multiplying by 2 doubles each number.
❓ Predict Output
intermediate2:00remaining
Using 'it' in filter lambda
What will this Kotlin code print?
Kotlin
val words = listOf("apple", "banana", "cherry", "date") val filtered = words.filter { it.length > 5 } println(filtered)
Attempts:
2 left
💡 Hint
The 'it' keyword is the current element in the list during filtering.
✗ Incorrect
The filter keeps words whose length is greater than 5. 'banana' and 'cherry' have length 6.
🔧 Debug
advanced2:00remaining
Identify the error with 'it' usage
What error does this Kotlin code produce?
Kotlin
val numbers = listOf(1, 2, 3) numbers.forEach { println(it) } numbers.forEach { val it = 5; println(it) }
Attempts:
2 left
💡 Hint
You cannot redeclare 'it' inside the lambda.
✗ Incorrect
The 'it' keyword is implicitly declared as the lambda parameter and cannot be redeclared inside the lambda body.
❓ Predict Output
advanced2:00remaining
Output of nested lambdas with 'it'
What is the output of this Kotlin code using nested lambdas with 'it'?
Kotlin
val list = listOf(1, 2, 3) val result = list.map { it * 2 }.filter { it > 3 } println(result)
Attempts:
2 left
💡 Hint
Each lambda has its own 'it' referring to the current element in that lambda.
✗ Incorrect
The first lambda doubles each number: [2,4,6]. The second lambda filters numbers greater than 3: [4,6].
🧠 Conceptual
expert2:00remaining
Understanding 'it' scope in Kotlin lambdas
Consider this Kotlin code snippet. What is the value of 'result' after execution?
Kotlin
val result = listOf(1, 2, 3).fold(0) { acc, it -> acc + it }
Attempts:
2 left
💡 Hint
In fold, the lambda has two parameters, so 'it' is not used implicitly.
✗ Incorrect
The fold function sums all elements starting from 0. The lambda uses 'acc' and 'it' explicitly, so 'it' is the current element.