Recall & Review
beginner
What is a function in Kotlin?
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?
✗ 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?
✗ 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?
✗ 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?
✗ 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 }?
✗ 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.