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
✗ 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) }?
AThe current element of the list
BThe index of the element
CThe entire list
DA Boolean value
✗ 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?
Alist.map { it * 2 }
Blist.map { a, b -> it + b }
Clist.filter { }
Dlist.forEach { println() }
✗ 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?
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
✗ Incorrect
When there are multiple parameters, you must name them explicitly in the lambda.
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
✗ 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.
Practice
(1/5)
1. What does the it keyword represent in a Kotlin lambda with a single parameter?
easy
A. The single implicit parameter of the lambda
B. A reserved word for loop counters
C. A keyword to declare variables
D. A function to print output
Solution
Step 1: Understand lambda parameters
In Kotlin, if a lambda has only one parameter, you don't need to name it explicitly.
Step 2: Role of it
The keyword it automatically refers to that single parameter inside the lambda.
Final Answer:
The single implicit parameter of the lambda -> Option A
Quick Check:
it = single lambda parameter [OK]
Hint: Remember: it means the one lambda input [OK]
Common Mistakes:
Thinking it is a loop variable
Confusing it with a keyword to declare variables
Assuming it prints output
2. Which of the following is the correct syntax to use it in a Kotlin lambda that doubles each number in a list?
easy
A. list.map { it * 2 }
B. list.map { x -> x * 2 }
C. list.map { it -> it * 2 }
D. list.map { * 2 }
Solution
Step 1: Identify lambda with single parameter
The lambda inside map has one parameter, so it can be used implicitly.
Step 2: Check syntax correctness
list.map { it * 2 } correctly uses it without naming the parameter explicitly.
Final Answer:
list.map { it * 2 } -> Option A
Quick Check:
Implicit single parameter lambda syntax = list.map { it * 2 } [OK]
Hint: Use it only when lambda has one parameter [OK]
Common Mistakes:
Using explicit parameter name and it together
Omitting parameter name but adding arrow
Trying to multiply without parameter
3. What is the output of this Kotlin code?
val numbers = listOf(1, 2, 3)
val result = numbers.filter { it > 1 }
println(result)
medium
A. [1, 2, 3]
B. [1]
C. [2, 3]
D. []
Solution
Step 1: Understand the filter condition
The lambda filters numbers where it > 1, so only numbers greater than 1 remain.
Step 2: Apply filter to the list
From the list [1, 2, 3], numbers 2 and 3 satisfy the condition.
Final Answer:
[2, 3] -> Option C
Quick Check:
Filter with it > 1 = [2, 3] [OK]
Hint: Filter keeps items where it meets condition [OK]
Common Mistakes:
Including 1 in the result
Confusing filter with map
Expecting original list unchanged
4. Identify the error in this Kotlin code snippet:
val list = listOf(1, 2, 3)
val doubled = list.map { it * }
println(doubled)
medium
A. Missing parameter name before *
B. No error, code runs fine
C. Using it without declaring lambda parameter
D. Syntax error: incomplete expression after *
Solution
Step 1: Analyze the lambda expression
The expression it * is incomplete; it lacks a value after the multiplication operator.
Step 2: Identify syntax error
This causes a syntax error because Kotlin expects a complete expression after *.
Final Answer:
Syntax error: incomplete expression after * -> Option D
Quick Check:
Incomplete expression causes syntax error [OK]
Hint: Check for complete expressions after operators [OK]
Common Mistakes:
Assuming it * is valid alone
Thinking it must be declared explicitly
Ignoring syntax errors
5. Given a list of strings val words = listOf("apple", "banana", "cat"), which Kotlin code uses it correctly to create a list of word lengths greater than 3?
hard
A. words.map { it.length > 3 }
B. words.filter { it.length > 3 }
C. words.map { it.length }.filter { it > 3 }
D. words.filter { length > 3 }.map { it }
Solution
Step 1: Understand the goal
We want words whose lengths are greater than 3, so we filter words based on their length.
Step 2: Analyze each option
words.filter { it.length > 3 }, words.filter { it.length > 3 }, correctly filters the words list to include only words with length greater than 3, using it properly.
Final Answer:
words.filter { it.length > 3 } -> Option B
Quick Check:
Filter words by length > 3 = words.filter { it.length > 3 } [OK]
Hint: Use filter with it.length > 3 to get words longer than 3 [OK]
Common Mistakes:
Filtering on string directly without length
Using it incorrectly in filter
Mixing order of map and filter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.