A function is a reusable block of code that performs a specific task. It can take inputs, called parameters, and can return a result.
Click to reveal answer
beginner
How do you declare a simple function in Kotlin that adds two numbers?
fun add(a: Int, b: Int): Int {
return a + b
}
Click to reveal answer
intermediate
What is a lambda expression in Kotlin?
A lambda is a small anonymous function that can be passed as a value. It is often used for short pieces of code like callbacks or operations on collections.
Click to reveal answer
intermediate
How do you write a lambda that multiplies a number by 2?
val double = { x: Int -> x * 2 }
Click to reveal answer
intermediate
What is the difference between a named function and a lambda in Kotlin?
A named function has a name and can be called anywhere by that name. A lambda is anonymous and usually used as a value passed to other functions.
Click to reveal answer
How do you declare a function in Kotlin that returns no value?
Afun greet() { println("Hi") }
Bfun greet(): Int { println("Hi") }
Cfun greet() -> Unit { println("Hi") }
Dfunction greet() { println("Hi") }
✗ Incorrect
In Kotlin, a function that returns no value has return type Unit, which can be omitted. So fun greet() { ... } is correct.
What symbol separates parameters and the body in a lambda expression?
A:
B->
C=
D=>
✗ Incorrect
In Kotlin lambdas, the arrow -> separates the parameter list from the body.
Which of these is a valid lambda that takes no parameters and returns 5?
A{ -> 5 }
Bfun() = 5
C{ x -> 5 }
D{ 5 }
✗ Incorrect
If a lambda has no parameters, you can omit the arrow and just write { 5 }.
How do you call a function named 'sum' with arguments 3 and 4?
Asum(3, 4)
Bsum 3, 4
Ccall sum(3, 4)
Dsum.call(3,4)
✗ Incorrect
Functions in Kotlin are called by their name followed by parentheses with arguments inside.
What is the type of the lambda val square = { x: Int -> x * x }?
AUnit
BInt
C(Int) -> Int
DFunction
✗ Incorrect
The lambda takes an Int and returns an Int, so its type is (Int) -> Int.
Explain how to declare and use a simple function in Kotlin with parameters and a return value.
Think about how you tell the app to do something with inputs and give back a result.
You got /5 concepts.
Describe what a lambda expression is and give an example of when you might use one in an Android app.
Imagine you want to quickly tell a button what to do when clicked without writing a full function.
You got /5 concepts.
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
Step 1: Understand lambda syntax
The code { a: Int, b: Int -> a + b } defines a function without a name.
Step 2: Identify lambda characteristics
This function is stored in the variable sum, showing it is unnamed but reusable.
Final Answer:
An unnamed function that can be stored in a variable -> Option B
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
Step 1: Review lambda syntax for no parameters
Lambdas with no parameters use { expression } syntax.
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.
Final Answer:
val greet = { "Hello" } -> Option D
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
Step 1: Understand the lambda function
The lambda { x: Int, y: Int -> x * y } multiplies two integers.
Step 2: Calculate the result of multiply(3, 4)
3 * 4 equals 12, so the printed output is 12.
Final Answer:
12 -> Option C
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:
D. Lambda must return a value, println returns Unit
Solution
Step 1: Check lambda syntax
The lambda takes a String parameter and calls println, which is allowed.
Step 2: Confirm return type flexibility
Lambdas can return Unit (like void), so no return type declaration is needed.
Final Answer:
No error, this is valid syntax -> Option A
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
Step 1: Understand filtering with lambda
The filter lambda should return true for even numbers using modulo operator.
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.
Final Answer:
numbers.filter { it % 2 == 0 } -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.