Challenge - 5 Problems
Trailing Lambda Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of trailing lambda usage in Kotlin
What is the output of this Kotlin code using the trailing lambda convention?
Kotlin
fun greet(name: String, action: () -> Unit) { println("Hello, $name!") action() } fun main() { greet("Alice") { println("Welcome to Kotlin.") } }
Attempts:
2 left
💡 Hint
Remember that trailing lambda syntax allows the lambda to be placed outside the parentheses.
✗ Incorrect
The function greet prints a greeting and then calls the lambda passed as the second argument. Using trailing lambda syntax, the lambda is placed outside the parentheses and executed, printing "Welcome to Kotlin." after the greeting.
❓ Predict Output
intermediate2:00remaining
Trailing lambda with multiple parameters
What will this Kotlin program print when using trailing lambda syntax with multiple parameters?
Kotlin
fun operate(x: Int, y: Int, operation: (Int, Int) -> Int) { val result = operation(x, y) println("Result: $result") } fun main() { operate(5, 3) { a, b -> a * b } }
Attempts:
2 left
💡 Hint
The lambda multiplies the two integers passed to it.
✗ Incorrect
The operate function takes two integers and a lambda that operates on them. The trailing lambda multiplies 5 and 3, resulting in 15.
🔧 Debug
advanced2:00remaining
Identify the error with trailing lambda usage
What error does this Kotlin code produce?
Kotlin
fun printMessage(message: String, printer: () -> Unit) { printer() println(message) } fun main() { printMessage("Hi") print { println("Printing...") } }
Attempts:
2 left
💡 Hint
Trailing lambda must be the last argument and placed immediately after parentheses.
✗ Incorrect
The code tries to call printMessage with a trailing lambda but places 'print' keyword incorrectly. The compiler expects the lambda immediately after the parentheses as the last argument. This causes a compilation error.
🧠 Conceptual
advanced2:00remaining
Understanding trailing lambda with multiple lambdas
Given a Kotlin function with two lambda parameters, which call correctly uses the trailing lambda convention?
Kotlin
fun process(a: Int, onSuccess: () -> Unit, onError: () -> Unit) { if (a > 0) onSuccess() else onError() } // Which call is correct?
Attempts:
2 left
💡 Hint
Only one lambda can be passed outside parentheses as trailing lambda.
✗ Incorrect
Kotlin allows only one trailing lambda, which must be the last argument. Option C correctly passes the first lambda inside parentheses and uses trailing syntax for the second lambda.
🚀 Application
expert3:00remaining
Refactor code using trailing lambda convention
Which option correctly refactors this Kotlin call to use the trailing lambda convention?
Kotlin
fun calculate(x: Int, y: Int, operation: (Int, Int) -> Int): Int { return operation(x, y) } val result = calculate(10, 20, { a, b -> a + b })
Attempts:
2 left
💡 Hint
Trailing lambda goes outside the parentheses as the last argument.
✗ Incorrect
Option B correctly moves the lambda outside the parentheses as a trailing lambda. Other options have syntax errors or invalid lambda placement.