0
0
Kotlinprogramming~5 mins

Passing lambdas to functions in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afun example(action: (Int) -> Unit)
Bfun example(action: () -> Unit)
Cfun example(action: () -> Int)
Dfun example(action: Unit)
Which of these is the correct way to call a function with a lambda as the last argument?
AdoSomething({ println("Hi") })
BdoSomething { println("Hi") }
CAll of the above
DdoSomething() { println("Hi") }
What keyword is used to define a lambda expression in Kotlin?
ANo keyword, just curly braces {}
Bfun
Cdef
Dlambda
What does the 'it' keyword represent inside a lambda with one parameter?
AThe lambda's parameter
BThe function name
CThe return value
DThe class instance
Why might you prefer passing a lambda instead of a named function?
ALambdas do not use memory
BLambdas are always faster
CNamed functions cannot be passed as parameters
DLambdas allow inline, concise code
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.