Complete the code to pass a lambda that prints "Hello".
fun greet(action: () -> Unit) {
action()
}
greet([1])The lambda must be passed as a block: { println("Hello") }.
Complete the code to pass a lambda that returns the sum of two integers.
fun operate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
return operation(a, b)
}
val result = operate(3, 4, [1])
println(result)The lambda must add the two parameters: { x, y -> x + y }.
Fix the error in passing a lambda that prints the given message.
fun printMessage(message: String, action: (String) -> Unit) {
action(message)
}
printMessage("Hi", [1])The lambda must explicitly take a parameter and print it: { msg -> println(msg) }.
Fill both blanks to create a lambda that filters even numbers and doubles them.
val numbers = listOf(1, 2, 3, 4, 5) val result = numbers.filter([1]).map([2]) println(result)
Filter keeps even numbers: { it % 2 == 0 }. Map doubles them: { it * 2 }.
Fill all three blanks to create a lambda that sorts strings by length and filters those longer than 3.
val words = listOf("cat", "house", "dog", "elephant") val filtered = words.sortedBy([1]).filter([2]).map([3]) println(filtered)
Sort by length: { it.length }. Filter words longer than 3: { it.length > 3 }. Map to uppercase: { it.uppercase() }.