0
0
Kotlinprogramming~20 mins

It keyword for single parameter in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
It Keyword Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[1, 2, 3, 4]
B[2, 4, 6, 8]
C[3, 6, 9, 12]
DCompilation error
Attempts:
2 left
💡 Hint
The 'it' keyword refers to the single parameter of the lambda function.
Predict Output
intermediate
2: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)
A["banana", "cherry"]
B["apple", "date"]
C["banana", "date"]
D[]
Attempts:
2 left
💡 Hint
The 'it' keyword is the current element in the list during filtering.
🔧 Debug
advanced
2: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) }
ACompilation error: 'it' cannot be redeclared
BRuntime error: NullPointerException
CCompilation error: Val cannot be reassigned
DNo error, prints 1 2 3 5 5 5
Attempts:
2 left
💡 Hint
You cannot redeclare 'it' inside the lambda.
Predict Output
advanced
2: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)
A[2, 4, 6]
B[1, 2, 3]
C[4, 6]
DCompilation error due to ambiguous 'it'
Attempts:
2 left
💡 Hint
Each lambda has its own 'it' referring to the current element in that lambda.
🧠 Conceptual
expert
2: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 }
ACompilation error: 'it' cannot be used with two parameters
B0
C3
D6
Attempts:
2 left
💡 Hint
In fold, the lambda has two parameters, so 'it' is not used implicitly.