Bird
Raised Fist0
Kotlinprogramming~10 mins

Passing lambdas to functions in Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to pass a lambda that prints "Hello".

Kotlin
fun greet(action: () -> Unit) {
    action()
}

greet([1])
Drag options to blanks, or click blank then click option'
A-> println("Hello")
Bprintln("Hello")
Cfun() { println("Hello") }
D{ println("Hello") }
Attempts:
3 left
💡 Hint
Common Mistakes
Passing println("Hello") directly without braces.
Using incorrect lambda syntax like '-> println("Hello")'.
2fill in blank
medium

Complete the code to pass a lambda that returns the sum of two integers.

Kotlin
fun operate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
    return operation(a, b)
}

val result = operate(3, 4, [1])
println(result)
Drag options to blanks, or click blank then click option'
A{ x, y -> x + y }
B{ x, y -> x - y }
C{ x, y -> x * y }
D{ x, y -> x / y }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction, multiplication, or division instead of addition.
Incorrect lambda parameter syntax.
3fill in blank
hard

Fix the error in passing a lambda that prints the given message.

Kotlin
fun printMessage(message: String, action: (String) -> Unit) {
    action(message)
}

printMessage("Hi", [1])
Drag options to blanks, or click blank then click option'
A{ println(it) }
B{ msg -> println(msg) }
Cprintln
D{ msg -> print(msg) }
Attempts:
3 left
💡 Hint
Common Mistakes
Passing println directly without matching the parameter.
Using { println(it) } without specifying the parameter name.
4fill in blank
hard

Fill both blanks to create a lambda that filters even numbers and doubles them.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.filter([1]).map([2])
println(result)
Drag options to blanks, or click blank then click option'
A{ it % 2 == 0 }
B{ it * 2 }
C{ it % 2 != 0 }
D{ it + 2 }
Attempts:
3 left
💡 Hint
Common Mistakes
Filtering odd numbers instead of even.
Adding 2 instead of multiplying by 2 in map.
5fill in blank
hard

Fill all three blanks to create a lambda that sorts strings by length and filters those longer than 3.

Kotlin
val words = listOf("cat", "house", "dog", "elephant")
val filtered = words.sortedBy([1]).filter([2]).map([3])
println(filtered)
Drag options to blanks, or click blank then click option'
A{ it.length }
B{ it.length > 3 }
C{ it.uppercase() }
D{ it.length < 3 }
Attempts:
3 left
💡 Hint
Common Mistakes
Filtering words shorter than 3 instead of longer.
Not converting words to uppercase in map.

Practice

(1/5)
1. What is a lambda in Kotlin when passed to a function?
easy
A. A small block of code that can be passed and executed inside the function
B. A type of variable that stores numbers
C. A special kind of loop
D. A keyword to declare a class

Solution

  1. Step 1: Understand what a lambda is

    A lambda is a small piece of code that you can write inline and pass around like a value.
  2. Step 2: Recognize its use in functions

    Functions can accept lambdas to customize behavior without changing the function code.
  3. Final Answer:

    A small block of code that can be passed and executed inside the function -> Option A
  4. Quick Check:

    Lambda = small code block passed to function [OK]
Hint: Lambdas are code blocks you pass to functions [OK]
Common Mistakes:
  • Thinking lambda is a variable type
  • Confusing lambda with loops
  • Believing lambda is a keyword for classes
2. Which of the following is the correct syntax to pass a lambda to a function in Kotlin?
easy
A. fun greet(action: Unit -> ()) { action() } greet { println("Hi") }
B. fun greet(action: () -> Unit) { action() } greet { println("Hi") }
C. fun greet(action: () -> Unit) { action } greet { println("Hi") }
D. fun greet(action: () -> Unit) { action() } greet(println("Hi"))

Solution

  1. Step 1: Check function parameter type

    The function expects a lambda with no parameters and no return value: () -> Unit.
  2. Step 2: Check how lambda is called and passed

    Inside the function, calling action() executes the lambda. Passing greet { println("Hi") } is correct syntax.
  3. Final Answer:

    fun greet(action: () -> Unit) { action() } greet { println("Hi") } -> Option B
  4. Quick Check:

    Correct lambda syntax = fun greet(action: () -> Unit) { action() } greet { println("Hi") } [OK]
Hint: Use () -> Unit and call lambda with () inside function [OK]
Common Mistakes:
  • Wrong lambda type syntax
  • Forgetting to call lambda with ()
  • Passing lambda as function argument incorrectly
3. What will be the output of this Kotlin code?
fun operate(x: Int, action: (Int) -> Int): Int {
    return action(x)
}

fun main() {
    val result = operate(5) { it * 2 }
    println(result)
}
medium
A. 5
B. 25
C. 10
D. Compilation error

Solution

  1. Step 1: Understand the operate function

    It takes an integer and a lambda that doubles the integer (it * 2).
  2. Step 2: Calculate the result

    Passing 5 to the lambda doubles it to 10, which is returned and printed.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    5 * 2 = 10 [OK]
Hint: Trace lambda input and output carefully [OK]
Common Mistakes:
  • Confusing input and output values
  • Forgetting lambda parameter 'it'
  • Assuming no return from lambda
4. Identify the error in this Kotlin code snippet:
fun applyOperation(x: Int, op: (Int) -> Int) {
    op(x)
}

fun main() {
    applyOperation(3) { println(it * 3) }
}
medium
A. No error, code runs fine
B. The function applyOperation should return Int but returns Unit
C. The lambda syntax is incorrect
D. The lambda passed does not return a value but function expects Int return

Solution

  1. Step 1: Check function signature and lambda

    applyOperation expects a lambda returning Int, but the lambda only prints and returns Unit.
  2. Step 2: Understand mismatch consequences

    Because the lambda returns Unit, but function expects Int, this causes a type mismatch error.
  3. Final Answer:

    The lambda passed does not return a value but function expects Int return -> Option D
  4. Quick Check:

    Lambda return type mismatch = The lambda passed does not return a value but function expects Int return [OK]
Hint: Match lambda return type with function parameter type [OK]
Common Mistakes:
  • Ignoring return type mismatch
  • Thinking println returns Int
  • Assuming no return needed for lambdas
5. You want to write a Kotlin function filterAndTransform that takes a list of integers and two lambdas: one to filter the list and one to transform each filtered item. Which of these function signatures correctly allows passing both lambdas?
hard
A. fun filterAndTransform(list: List<Int>, filter: (Int) -> Boolean, transform: (Int) -> Int): List<Int>
B. fun filterAndTransform(list: List<Int>, filter: Int, transform: Int): List<Int>
C. fun filterAndTransform(list: List<Int>, filter: (Int) -> Int, transform: (Int) -> Boolean): List<Int>
D. fun filterAndTransform(list: List<Int>, filter: () -> Boolean, transform: () -> Int): List<Int>

Solution

  1. Step 1: Identify correct lambda types for filtering and transforming

    Filtering needs a lambda that takes Int and returns Boolean; transforming needs a lambda that takes Int and returns Int.
  2. Step 2: Match function signature with these lambda types

    fun filterAndTransform(list: List<Int>, filter: (Int) -> Boolean, transform: (Int) -> Int): List<Int> matches these types correctly; others have wrong parameter or return types.
  3. Final Answer:

    fun filterAndTransform(list: List<Int>, filter: (Int) -> Boolean, transform: (Int) -> Int): List<Int> -> Option A
  4. Quick Check:

    Filter returns Boolean, transform returns Int [OK]
Hint: Filter lambda returns Boolean, transform lambda returns transformed value [OK]
Common Mistakes:
  • Using wrong lambda parameter or return types
  • Confusing filter and transform lambda roles
  • Using lambdas with no parameters
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