Bird
Raised Fist0
Android Kotlinmobile~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is a lambda function in Kotlin?
val sum = { a: Int, b: Int -> a + b }
easy
A. A function that must have a name and cannot be stored
B. An unnamed function that can be stored in a variable
C. A special class for storing data
D. A type of loop for repeating code

Solution

  1. Step 1: Understand lambda syntax

    The code { a: Int, b: Int -> a + b } defines a function without a name.
  2. Step 2: Identify lambda characteristics

    This function is stored in the variable sum, showing it is unnamed but reusable.
  3. Final Answer:

    An unnamed function that can be stored in a variable -> Option B
  4. Quick Check:

    Lambda = unnamed function stored in variable [OK]
Hint: Lambdas have no name and are assigned to variables [OK]
Common Mistakes:
  • Thinking lambdas must have names
  • Confusing lambdas with classes
  • Assuming lambdas are loops
2. Which of the following is the correct syntax to declare a lambda that takes no parameters and returns a String "Hello"?
easy
A. val greet = fun() { return "Hello" }
B. val greet = () -> "Hello"
C. val greet = { -> "Hello"
D. val greet = { "Hello" }

Solution

  1. Step 1: Review lambda syntax for no parameters

    Lambdas with no parameters use { expression } syntax.
  2. Step 2: Check each option

    val greet = { "Hello" } correctly uses { "Hello" }. val greet = { -> "Hello" missing closing brace, B is function type syntax, D is a named function.
  3. Final Answer:

    val greet = { "Hello" } -> Option D
  4. Quick Check:

    No-parameter lambda = { value } [OK]
Hint: Use { expression } for lambdas with no parameters [OK]
Common Mistakes:
  • Missing closing brace in lambda
  • Confusing lambda with function type syntax
  • Using fun keyword inside lambda
3. What is the output of this Kotlin code?
val multiply = { x: Int, y: Int -> x * y }
println(multiply(3, 4))
medium
A. 7
B. 34
C. 12
D. Error

Solution

  1. Step 1: Understand the lambda function

    The lambda { x: Int, y: Int -> x * y } multiplies two integers.
  2. Step 2: Calculate the result of multiply(3, 4)

    3 * 4 equals 12, so the printed output is 12.
  3. Final Answer:

    12 -> Option C
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Multiply inputs inside lambda to get output [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Concatenating numbers as strings
  • Expecting syntax error
4. Identify the error in this Kotlin lambda declaration:
val greet = { name: String ->
  println("Hello, " + name)
}
medium
A. No error, this is valid syntax
B. Cannot use println inside lambda
C. Missing return type declaration
D. Lambda must return a value, println returns Unit

Solution

  1. Step 1: Check lambda syntax

    The lambda takes a String parameter and calls println, which is allowed.
  2. Step 2: Confirm return type flexibility

    Lambdas can return Unit (like void), so no return type declaration is needed.
  3. Final Answer:

    No error, this is valid syntax -> Option A
  4. Quick Check:

    Lambda with println is valid [OK]
Hint: Lambdas can return Unit; println is allowed [OK]
Common Mistakes:
  • Expecting return type declaration
  • Thinking println is disallowed
  • Confusing Unit with error
5. You want to filter a list of numbers to keep only even numbers using a lambda. Which code correctly does this?
val numbers = listOf(1, 2, 3, 4, 5, 6)
val evens = numbers.filter( /* fill here */ )
println(evens)
hard
A. numbers.filter { it % 2 == 0 }
B. numbers.filter { x -> x / 2 == 0 }
C. numbers.filter { it % 2 = 0 }
D. numbers.filter { it & 2 == 0 }

Solution

  1. Step 1: Understand filtering with lambda

    The filter lambda should return true for even numbers using modulo operator.
  2. Step 2: Evaluate each option

    numbers.filter { it % 2 == 0 } correctly uses it % 2 == 0. numbers.filter { x -> x / 2 == 0 } uses division instead of modulo, C uses assignment (=) instead of comparison (==), A uses bitwise AND incorrectly.
  3. Final Answer:

    numbers.filter { it % 2 == 0 } -> Option A
  4. Quick Check:

    Filter even numbers with modulo == 0 [OK]
Hint: Use it % 2 == 0 to check even numbers in filter [OK]
Common Mistakes:
  • Using = instead of == for comparison
  • Using division instead of modulo
  • Misusing bitwise operators
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard