Challenge - 5 Problems
Lambda Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Identify the output of a lambda expression
What is the output of this Kotlin code snippet using a lambda function?
Android Kotlin
val numbers = listOf(1, 2, 3, 4) val doubled = numbers.map { it * 2 } println(doubled)
Attempts:
2 left
💡 Hint
Remember that map applies the lambda to each element and returns a new list.
✗ Incorrect
The map function applies the lambda 'it * 2' to each element, doubling each number.
❓ ui_behavior
intermediate2:00remaining
Lambda usage in button click listener
Given this Kotlin Android code, what happens when the button is clicked?
Android Kotlin
button.setOnClickListener {
textView.text = "Clicked!"
}Attempts:
2 left
💡 Hint
setOnClickListener takes a lambda with no parameters to handle clicks.
✗ Incorrect
The lambda updates the textView's text property when the button is clicked.
❓ lifecycle
advanced2:00remaining
Effect of lambda capturing variables in Android lifecycle
Consider this Kotlin code inside an Activity:
var count = 0
button.setOnClickListener {
count++
textView.text = count.toString()
}
What happens to the count variable if the device is rotated (causing Activity recreation)?
Attempts:
2 left
💡 Hint
Think about what happens to Activity variables on configuration changes.
✗ Incorrect
When the Activity is recreated, all instance variables including count reset to initial values.
advanced
2:00remaining
Passing lambda as a callback between fragments
In Kotlin Android, you want to pass a lambda from Fragment A to Fragment B to handle a button click in Fragment B. Which approach correctly passes and uses the lambda?
Attempts:
2 left
💡 Hint
Lambdas are not Parcelable, so passing via Bundle is not possible.
✗ Incorrect
You can define a lambda property in Fragment B and assign it from Fragment A before navigating. Then Fragment B calls it on button click.
🔧 Debug
expert2:00remaining
Diagnose the cause of a NullPointerException with lambda usage
This Kotlin code crashes with a NullPointerException when the button is clicked:
var listener: (() -> Unit)? = null
button.setOnClickListener {
listener?.invoke()
}
// Somewhere else, listener is never assigned.
What is the cause of the crash?
Attempts:
2 left
💡 Hint
Check how the safe call operator ?. works in Kotlin.
✗ Incorrect
The safe call operator ?. prevents invocation if listener is null, so no NullPointerException occurs here. The crash must be caused elsewhere.