0
0
Kotlinprogramming~20 mins

Trailing lambda convention in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Trailing Lambda Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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.")
    }
}
AHello, Alice!\nWelcome to Kotlin.
BHello, Alice!\n() -> Unit
CHello, Alice!\nWelcome to Kotlin.\n() -> Unit
DCompilation error due to lambda syntax
Attempts:
2 left
💡 Hint
Remember that trailing lambda syntax allows the lambda to be placed outside the parentheses.
Predict Output
intermediate
2: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 }
}
AResult: 15
BResult: 8
CResult: 53
DCompilation error due to lambda parameters
Attempts:
2 left
💡 Hint
The lambda multiplies the two integers passed to it.
🔧 Debug
advanced
2: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...") }
}
ANo error, prints 'Printing...' then 'Hi'
BSyntaxError: Unexpected token 'print'
CRuntimeException: Null pointer exception
DCompilation error: Lambda outside parentheses must be last argument
Attempts:
2 left
💡 Hint
Trailing lambda must be the last argument and placed immediately after parentheses.
🧠 Conceptual
advanced
2: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?
Aprocess(5) { println("Success") } { println("Error") }
Bprocess(5) { println("Success") }, { println("Error") }
Cprocess(5, { println("Success") }) { println("Error") }
Dprocess(5, { println("Success") }, { println("Error") })
Attempts:
2 left
💡 Hint
Only one lambda can be passed outside parentheses as trailing lambda.
🚀 Application
expert
3: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 })
Aval result = calculate(10, 20) (a, b) -> a + b
Bval result = calculate(10, 20) { a, b -> a + b }
Cval result = calculate(10, 20) { a, b -> a + b }()
Dval result = calculate(10, 20, a, b -> a + b)
Attempts:
2 left
💡 Hint
Trailing lambda goes outside the parentheses as the last argument.