0
0
Kotlinprogramming~5 mins

It keyword for single parameter in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the it keyword represent in Kotlin lambdas with a single parameter?
In Kotlin, it is an implicit name for the single parameter in a lambda expression when the parameter name is not explicitly specified.
Click to reveal answer
beginner
How do you use it in a Kotlin lambda to print each element of a list?
You can write list.forEach { println(it) }. Here, it refers to each element of the list during iteration.
Click to reveal answer
intermediate
Can you use it if your lambda has more than one parameter?
No. The it keyword is only available when the lambda has exactly one parameter. For multiple parameters, you must name them explicitly.
Click to reveal answer
beginner
Rewrite this lambda using it: list.map { number -> number * 2 }
Using it, it becomes list.map { it * 2 }. This works because the lambda has a single parameter.
Click to reveal answer
beginner
Why is using it helpful in Kotlin lambdas?
Using it makes the code shorter and cleaner when the lambda has only one parameter, improving readability.
Click to reveal answer
In Kotlin, when can you use the it keyword inside a lambda?
AWhen the lambda has no parameters
BWhen the lambda has exactly one parameter
CWhen the lambda has multiple parameters
DOnly in functions, not lambdas
What does it represent in this code: list.forEach { println(it) }?
AThe current element of the list
BThe index of the element
CThe entire list
DA Boolean value
Which of these is a correct use of it in Kotlin?
Alist.map { it * 2 }
Blist.map { a, b -> it + b }
Clist.filter { }
Dlist.forEach { println() }
If a lambda has two parameters, how do you refer to them?
AUse <code>it</code> for the first and <code>that</code> for the second
BYou cannot use lambdas with two parameters
CUse <code>it</code> for both
DYou must name both parameters explicitly
What is the benefit of using it in Kotlin lambdas?
AImproves performance
BAllows lambdas to have multiple parameters
CMakes code shorter and easier to read for single-parameter lambdas
DAutomatically types the parameter
Explain how the it keyword works in Kotlin lambdas and when you can use it.
Think about how Kotlin lets you skip naming the parameter when there is only one.
You got /4 concepts.
    Give an example of a Kotlin lambda using it and explain what it refers to in your example.
    Try a simple list operation like forEach or map.
    You got /3 concepts.