0
0
Kotlinprogramming~20 mins

Why lambdas enable functional style in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lambda Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin lambda expression?
Consider the following Kotlin code using a lambda. What will it print?
Kotlin
val numbers = listOf(1, 2, 3, 4)
val doubled = numbers.map { it * 2 }
println(doubled)
A[1, 4, 9, 16]
B[1, 2, 3, 4]
C[2, 4, 6, 8]
DCompilation error
Attempts:
2 left
💡 Hint
The map function applies the lambda to each element.
🧠 Conceptual
intermediate
2:00remaining
Why do lambdas support functional programming in Kotlin?
Which statement best explains why lambdas enable a functional style in Kotlin?
ALambdas enforce mutable state for variables.
BLambdas automatically optimize code for faster execution.
CLambdas replace all loops with recursion.
DLambdas allow functions to be treated as values, enabling passing and returning functions.
Attempts:
2 left
💡 Hint
Think about how functions can be used in functional programming.
🔧 Debug
advanced
2:00remaining
What error does this Kotlin code with lambda cause?
What error will this Kotlin code produce when run? val list = listOf(1, 2, 3) val result = list.filter { it > 1 } .map { it.toString() } .reduce { acc, s -> acc + s } println(result)
Kotlin
val list = listOf(1, 2, 3)
val result = list.filter { it > 1 }
    .map { it.toString() }
    .reduce { acc, s -> acc + s }
println(result)
ACompilation error: reduce requires non-empty list
BNo error, prints "23"
CRuntimeException: UnsupportedOperationException
DNo error, prints "223"
Attempts:
2 left
💡 Hint
Check what filter and map produce before reduce.
Predict Output
advanced
2:00remaining
What is the output of this Kotlin code using lambdas and sequences?
What will this Kotlin code print? val seq = sequenceOf(1, 2, 3, 4) val result = seq.filter { it % 2 == 0 } .map { it * it } .toList() println(result)
Kotlin
val seq = sequenceOf(1, 2, 3, 4)
val result = seq.filter { it % 2 == 0 }
    .map { it * it }
    .toList()
println(result)
A[4, 16]
B[1, 4, 9, 16]
C[2, 4]
DCompilation error: sequence operations not allowed
Attempts:
2 left
💡 Hint
Filter keeps even numbers, map squares them.
🚀 Application
expert
2:00remaining
How many items are in the resulting list after this Kotlin lambda chain?
Given this Kotlin code, how many items are in the final list? val data = listOf("apple", "banana", "cherry", "date") val filtered = data.filter { it.length > 5 } val mapped = filtered.map { it.uppercase() } val result = mapped.drop(1) println(result.size)
Kotlin
val data = listOf("apple", "banana", "cherry", "date")
val filtered = data.filter { it.length > 5 }
val mapped = filtered.map { it.uppercase() }
val result = mapped.drop(1)
println(result.size)
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Count items with length > 5, then drop the first.