0
0
Kotlinprogramming~5 mins

Lambda syntax and declaration 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 declare a simple lambda that takes two integers and returns their sum?
You write: { a: Int, b: Int -> a + b }. This means the lambda takes two integers a and b and returns their sum.
Click to reveal answer
beginner
What is the syntax to assign a lambda to a variable in Kotlin?
You assign it like this: val sum: (Int, Int) -> Int = { a, b -> a + b }. Now sum holds the lambda and you can call it like a function.
Click to reveal answer
beginner
How do you call a lambda stored in a variable?
If you have val greet = { name: String -> "Hello, $name!" }, you call it like this: greet("Alice").
Click to reveal answer
intermediate
What is the implicit name for a single parameter in a Kotlin lambda?
If a lambda has only one parameter, you can use it instead of naming it explicitly. For example: { it * 2 } doubles the input.
Click to reveal answer
Which of the following is a correct Kotlin lambda syntax for adding two numbers?
A{ x: Int, y: Int -> x + y }
B(x, y) => x + y
Cfun(x, y) { return x + y }
Dlambda x, y: x + y
How do you call a lambda stored in a variable named multiply with arguments 3 and 4?
Amultiply->(3,4)
Bmultiply.call(3, 4)
Cmultiply(3, 4)
Dcall multiply(3,4)
What does the it keyword represent in a Kotlin lambda?
AThe single implicit parameter of the lambda
BThe return value of the lambda
CA keyword to exit the lambda
DA variable declared outside the lambda
Which of these is NOT a valid way to declare a lambda in Kotlin?
Aval f = { x: Int -> x * 2 }
Bval f: (Int) -> Int = { x -> x * 2 }
Cval f = fun(x: Int) = x * 2
Dval f = lambda x: x * 2
What is the return type of a Kotlin lambda that looks like { a: Int, b: Int -> a + b }?
ANothing
BInt
CString
DUnit
Explain how to declare and use a lambda in Kotlin that takes one parameter and returns its square.
Think about how to write the lambda and how to call it with a number.
You got /4 concepts.
    Describe the difference between a lambda expression and a regular function in Kotlin.
    Focus on how lambdas are unnamed and can be stored or passed around easily.
    You got /4 concepts.