0
0
Android Kotlinmobile~20 mins

Functions and lambdas in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lambda Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2: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)
ACompilation error
B[1, 2, 3, 4]
C[1, 4, 9, 16]
D[2, 4, 6, 8]
Attempts:
2 left
💡 Hint
Remember that map applies the lambda to each element and returns a new list.
ui_behavior
intermediate
2: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!"
}
AThe app crashes because the lambda is missing a parameter.
BThe textView text changes to "Clicked!" when the button is clicked.
CNothing happens because the listener is not set properly.
DThe button text changes to "Clicked!" instead of textView.
Attempts:
2 left
💡 Hint
setOnClickListener takes a lambda with no parameters to handle clicks.
lifecycle
advanced
2: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)?
Acount keeps its value because lambdas preserve variable state across recreations.
Bcount increments twice on each click after rotation due to lambda duplication.
Ccount resets to 0 because the Activity is recreated and variables are reinitialized.
DThe app crashes because count is accessed inside a lambda.
Attempts:
2 left
💡 Hint
Think about what happens to Activity variables on configuration changes.
navigation
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?
ADefine a lambda property in Fragment B and set it from Fragment A before navigation; call it inside Fragment B's button listener.
BPass the lambda as a Bundle argument during navigation and retrieve it in Fragment B.
CUse a global variable to store the lambda accessible by both fragments.
DLambdas cannot be passed between fragments; use interfaces instead.
Attempts:
2 left
💡 Hint
Lambdas are not Parcelable, so passing via Bundle is not possible.
🔧 Debug
expert
2: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?
AThe safe call operator ?. prevents invocation if listener is null, so no crash occurs.
Blistener is null, so invoking it causes a NullPointerException.
Cbutton is null, causing the crash when setOnClickListener is called.
DThe lambda assigned to listener throws an exception internally.
Attempts:
2 left
💡 Hint
Check how the safe call operator ?. works in Kotlin.