Bird
Raised Fist0
Kotlinprogramming~20 mins

Trailing lambda convention in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the trailing lambda convention in Kotlin?
easy
A. To allow placing the lambda expression outside the parentheses when it is the last argument
B. To require all lambda expressions to be inside parentheses
C. To prevent using lambda expressions as function arguments
D. To force lambda expressions to be declared as variables before use

Solution

  1. Step 1: Understand the trailing lambda convention and compare options

    The trailing lambda convention lets you write a lambda expression outside the parentheses if it is the last argument of a function. Only "To allow placing the lambda expression outside the parentheses when it is the last argument" correctly describes this behavior. Other options contradict the convention.
  2. Final Answer:

    To allow placing the lambda expression outside the parentheses when it is the last argument -> Option A
  3. Quick Check:

    Trailing lambda = lambda outside parentheses [OK]
Hint: Last lambda argument can go outside parentheses [OK]
Common Mistakes:
  • Thinking lambdas must always be inside parentheses
  • Confusing trailing lambda with lambda variable declaration
  • Believing trailing lambda disallows lambdas as arguments
2. Which of the following Kotlin function calls correctly uses the trailing lambda convention?
easy
A. list.filter { it > 0 }
B. list.filter({ it > 0 })
C. list.filter(it > 0)
D. list.filter it > 0

Solution

  1. Step 1: Identify correct trailing lambda syntax and check other options

    Trailing lambda means the lambda is placed outside the parentheses. list.filter { it > 0 } shows this correctly. list.filter({ it > 0 }) uses lambda inside parentheses (valid but not trailing). list.filter(it > 0) and D are syntax errors.
  2. Final Answer:

    list.filter { it > 0 } -> Option A
  3. Quick Check:

    Trailing lambda = lambda outside parentheses [OK]
Hint: Trailing lambda goes after parentheses, no extra parentheses needed [OK]
Common Mistakes:
  • Putting lambda inside parentheses when trailing lambda is expected
  • Omitting parentheses incorrectly
  • Writing lambda without braces
3. What is the output of this Kotlin code?
fun greet(action: () -> String) = println(action())
greet { "Hello, Kotlin!" }
medium
A. No output
B. kotlin.Unit
C. Syntax error
D. Hello, Kotlin!

Solution

  1. Step 1: Understand the function call and evaluate the output

    The function greet takes a lambda returning a String and prints its result. The call uses trailing lambda syntax. The lambda returns "Hello, Kotlin!" which is printed by println.
  2. Final Answer:

    Hello, Kotlin! -> Option D
  3. Quick Check:

    Trailing lambda call prints lambda result [OK]
Hint: Trailing lambda calls run the lambda and print result [OK]
Common Mistakes:
  • Expecting syntax error due to trailing lambda
  • Confusing return value with Unit
  • Missing that println prints the lambda result
4. Identify the error in this Kotlin code using trailing lambda:
fun calculate(x: Int, operation: (Int) -> Int): Int {
    return operation(x)
}

val result = calculate(5) operation { it * 2 }
medium
A. Missing parentheses around lambda argument
B. Incorrect function declaration
C. Trailing lambda must be outside parentheses but parentheses are missing
D. No error, code is correct

Solution

  1. Step 1: Analyze the function call syntax and correct trailing lambda usage

    The function call calculate(5) operation { it * 2 } is invalid because the trailing lambda must be placed immediately after the parentheses without extra tokens. The correct call is calculate(5) { it * 2 }. The word operation should not appear between parentheses and lambda.
  2. Final Answer:

    Trailing lambda must be outside parentheses but parentheses are missing -> Option C
  3. Quick Check:

    Trailing lambda goes right after parentheses [OK]
Hint: Trailing lambda must follow parentheses directly, no extra tokens [OK]
Common Mistakes:
  • Inserting parameter name between parentheses and lambda
  • Forgetting to remove parentheses when using trailing lambda
  • Confusing function parameter names with call syntax
5. Given this Kotlin function:
fun processNumbers(numbers: List<Int>, filter: (Int) -> Boolean, action: (Int) -> Unit) {
    for (number in numbers) {
        if (filter(number)) action(number)
    }
}

Which is the correct way to call processNumbers using trailing lambda convention for both lambdas?
hard
A. processNumbers(listOf(1,2,3)) { it > 1 } { println(it) }
B. processNumbers(listOf(1,2,3), { it > 1 }) { println(it) }
C. processNumbers(listOf(1,2,3)) { println(it) } { it > 1 }
D. processNumbers(listOf(1,2,3), { println(it) }, { it > 1 })

Solution

  1. Step 1: Understand trailing lambda rules for multiple lambdas and apply to function call

    Kotlin allows only the last lambda argument to be passed outside parentheses as a trailing lambda. Since action is the last lambda, it can be trailing. The first lambda filter must be inside parentheses. processNumbers(listOf(1,2,3), { it > 1 }) { println(it) } correctly places filter inside parentheses and action outside.
  2. Final Answer:

    processNumbers(listOf(1,2,3), { it > 1 }) { println(it) } -> Option B
  3. Quick Check:

    Only last lambda can be trailing [OK]
Hint: Only last lambda argument goes outside parentheses [OK]
Common Mistakes:
  • Trying to put multiple lambdas outside parentheses
  • Swapping lambda order incorrectly
  • Putting non-lambda arguments outside parentheses
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard