Recall & Review
beginner
What is a lambda expression in Kotlin?
A lambda expression is a small block of code that can be passed around and executed later. It is like a mini-function without a name.
Click to reveal answer
beginner
How do you pass a lambda to a function in Kotlin?
You define a function parameter with a function type, then pass the lambda when calling the function. For example: fun doSomething(action: () -> Unit) { action() }
Click to reveal answer
beginner
What is the syntax to call a function with a lambda as the last argument?
If the lambda is the last argument, you can put it outside the parentheses. Example: doSomething() { println("Hello") }
Click to reveal answer
intermediate
Explain the difference between passing a lambda and a regular function as a parameter.
A lambda is an anonymous function defined inline, while a regular function has a name and is defined separately. Both can be passed as parameters if the types match.
Click to reveal answer
beginner
What is the benefit of using lambdas when passing functions?
Lambdas make code concise and flexible. They allow you to write small pieces of behavior inline without creating separate named functions.
Click to reveal answer
How do you declare a function parameter that accepts a lambda with no parameters and no return value?
✗ Incorrect
The correct syntax for a lambda with no parameters and no return value is () -> Unit.
Which of these is the correct way to call a function with a lambda as the last argument?
✗ Incorrect
All these are valid ways to pass a lambda as the last argument in Kotlin.
What keyword is used to define a lambda expression in Kotlin?
✗ Incorrect
Lambdas are defined using curly braces {} without a special keyword.
What does the 'it' keyword represent inside a lambda with one parameter?
✗ Incorrect
'it' is the implicit name for the single parameter in a lambda.
Why might you prefer passing a lambda instead of a named function?
✗ Incorrect
Lambdas let you write short code inline, making your code cleaner and easier to read.
Explain how to pass a lambda to a Kotlin function and call it inside the function.
Think about how you define the parameter and how you use it inside.
You got /3 concepts.
Describe the benefits of using lambdas when passing behavior to functions.
Why do programmers like lambdas for small tasks?
You got /4 concepts.