0
0
Kotlinprogramming~20 mins

Lambda syntax and declaration in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Lambda Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin lambda expression?
Consider the following Kotlin code snippet. What will be printed when it runs?
Kotlin
val multiplyByTwo = { x: Int -> x * 2 }
println(multiplyByTwo(5))
A10
B25
C5
DCompilation error
Attempts:
2 left
💡 Hint
Remember the lambda takes an integer and multiplies it by 2.
Predict Output
intermediate
2:00remaining
What does this Kotlin lambda return?
What is the output of this Kotlin code?
Kotlin
val greet: (String) -> String = { name -> "Hello, $name!" }
println(greet("Anna"))
AHello, name!
BHello, Anna!
Cgreet
DCompilation error
Attempts:
2 left
💡 Hint
The lambda uses string interpolation with the parameter name.
Predict Output
advanced
2:00remaining
What is the output of this Kotlin lambda with implicit it?
What will this Kotlin code print?
Kotlin
val square: (Int) -> Int = { it * it }
println(square(4))
A16
B4
CItself (function reference)
DCompilation error
Attempts:
2 left
💡 Hint
When a lambda has one parameter, you can use 'it' to refer to it.
Predict Output
advanced
2:00remaining
What is the output of this Kotlin lambda with multiple parameters?
What will this Kotlin code print?
Kotlin
val sum: (Int, Int) -> Int = { a, b -> a + b }
println(sum(3, 7))
A37
B3
CCompilation error
D10
Attempts:
2 left
💡 Hint
The lambda adds two integers a and b.
🧠 Conceptual
expert
3:00remaining
Which Kotlin lambda declaration correctly defines a lambda that returns the length of a string?
Choose the correct Kotlin lambda declaration that takes a String and returns its length as an Int.
Aval lengthLambda: (String) -> String = { s -> s.length }
Bval lengthLambda = { s: String -> s.length }
Cval lengthLambda: (String) -> Int = { s -> s.length }
Dval lengthLambda: (Int) -> Int = { s -> s.length }
Attempts:
2 left
💡 Hint
Check the lambda parameter type and return type carefully.