Challenge - 5 Problems
Lambda Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a lambda passed to a function
What is the output of this Kotlin code when the lambda is passed to the function?
Kotlin
fun operateOnNumber(x: Int, operation: (Int) -> Int): Int { return operation(x) } fun main() { val result = operateOnNumber(5) { it * 3 } println(result) }
Attempts:
2 left
💡 Hint
Remember that the lambda multiplies the input by 3.
✗ Incorrect
The function operateOnNumber takes an integer and a lambda that multiplies the integer by 3. Passing 5 results in 5 * 3 = 15.
❓ Predict Output
intermediate2:00remaining
Lambda with multiple parameters
What will be printed by this Kotlin program?
Kotlin
fun combineStrings(a: String, b: String, combiner: (String, String) -> String): String { return combiner(a, b) } fun main() { val result = combineStrings("Hello", "World") { x, y -> "$x $y!" } println(result) }
Attempts:
2 left
💡 Hint
Look at how the lambda formats the strings with a space and exclamation mark.
✗ Incorrect
The lambda combines the two strings with a space and adds an exclamation mark, resulting in "Hello World!".
🔧 Debug
advanced2:00remaining
Identify the error when passing a lambda
This Kotlin code does not compile. What is the cause of the error?
Kotlin
fun applyOperation(x: Int, operation: (Int) -> Int): Int { return operation(x) } fun main() { val result = applyOperation(10) { it + 5 } println(result) }
Attempts:
2 left
💡 Hint
Check the function parameter type declaration syntax for lambdas.
✗ Incorrect
The parameter type for the lambda must be declared as (Int) -> Int, not Int -> Int. The missing parentheses cause a type mismatch error.
❓ Predict Output
advanced2:00remaining
Lambda capturing external variable
What is the output of this Kotlin code?
Kotlin
fun main() { var counter = 0 val increment: () -> Int = { counter += 1; counter } println(increment()) println(increment()) println(counter) }
Attempts:
2 left
💡 Hint
The lambda modifies the external variable counter each time it is called.
✗ Incorrect
Each call to increment increases counter by 1 and returns the new value. So first call returns 1, second returns 2, and counter is 2 at the end.
🧠 Conceptual
expert2:00remaining
Effect of inline lambdas on performance
Which statement about inline lambdas in Kotlin is true?
Attempts:
2 left
💡 Hint
Think about how inlining affects function calls and memory allocation.
✗ Incorrect
Inlining lambdas replaces the lambda call with the lambda body, avoiding creating function objects and call overhead, improving performance.