0
0
Kotlinprogramming~5 mins

Trailing lambda convention in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the trailing lambda convention in Kotlin?
It is a Kotlin syntax feature that allows you to pass a lambda expression outside the parentheses of a function call if the lambda is the last argument.
Click to reveal answer
beginner
How do you call a function with a trailing lambda in Kotlin?
You write the lambda expression after the function call parentheses instead of inside them, making the code cleaner and more readable.
Click to reveal answer
beginner
Example: Convert this call to use trailing lambda:
list.filter({ it > 5 })
list.filter { it > 5 }
The lambda is moved outside the parentheses because it is the last argument.
Click to reveal answer
intermediate
Can you use trailing lambda if the lambda is not the last argument?
No, trailing lambda syntax only works if the lambda is the last argument in the function call.
Click to reveal answer
beginner
Why is trailing lambda convention useful?
It makes code easier to read and write, especially when lambdas are long or nested, by reducing parentheses clutter.
Click to reveal answer
What does the trailing lambda convention allow in Kotlin?
ACalling functions without parentheses
BUsing lambdas only inside parentheses
CDefining lambdas without curly braces
DPassing a lambda expression outside the parentheses if it is the last argument
Which of these is a correct use of trailing lambda?
Alist.map { it * 2 }
Blist.map({ it * 2 })
Clist.map it * 2
Dlist.map it => it * 2
Can you use trailing lambda if the function has multiple arguments and the lambda is not last?
AYes, always
BNo, only if lambda is last argument
COnly if lambda is first argument
DOnly if function has one argument
What is a benefit of using trailing lambda?
AMakes code more readable and less cluttered
BMakes code run faster
CAllows lambdas without curly braces
DRemoves need for function names
Which Kotlin feature is demonstrated by this code?
button.setOnClickListener { println("Clicked") }
AExtension function
BData class
CTrailing lambda convention
DObject declaration
Explain the trailing lambda convention in Kotlin and when you can use it.
Think about how lambdas are passed to functions.
You got /3 concepts.
    Give an example of a function call using trailing lambda and explain why it is valid.
    Try using list.filter or similar function.
    You got /3 concepts.