Challenge - 5 Problems
Kotlin Lambda Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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))
Attempts:
2 left
💡 Hint
Remember the lambda takes an integer and multiplies it by 2.
✗ Incorrect
The lambda takes an integer x and returns x multiplied by 2. So multiplyByTwo(5) returns 10.
❓ Predict Output
intermediate2: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"))
Attempts:
2 left
💡 Hint
The lambda uses string interpolation with the parameter name.
✗ Incorrect
The lambda takes a string and returns "Hello, " plus the name plus "!". So it prints "Hello, Anna!".
❓ Predict Output
advanced2: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))
Attempts:
2 left
💡 Hint
When a lambda has one parameter, you can use 'it' to refer to it.
✗ Incorrect
The lambda squares the input number. 4 squared is 16, so it prints 16.
❓ Predict Output
advanced2: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))
Attempts:
2 left
💡 Hint
The lambda adds two integers a and b.
✗ Incorrect
The lambda adds 3 and 7, resulting in 10, which is printed.
🧠 Conceptual
expert3: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.
Attempts:
2 left
💡 Hint
Check the lambda parameter type and return type carefully.
✗ Incorrect
Option C correctly declares a lambda taking a String and returning an Int (the length). Option C misses explicit type but is valid Kotlin, but since the question asks for declaration with type, A is best. Option C returns String but length is Int, so wrong return type. Option C takes Int but tries to get length, which is invalid.