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?✗ Incorrect
it is the implicit name for the single parameter in a lambda with exactly one parameter.
What does
it represent in this code: list.forEach { println(it) }?✗ Incorrect
it refers to each element of the list as the lambda runs for each item.
Which of these is a correct use of
it in Kotlin?✗ Incorrect
it can only be used when the lambda has one parameter, like in list.map { it * 2 }.
If a lambda has two parameters, how do you refer to them?
✗ Incorrect
When there are multiple parameters, you must name them explicitly in the lambda.
What is the benefit of using
it in Kotlin lambdas?✗ Incorrect
it helps keep code concise and clean when the lambda has one 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.