Bird
Raised Fist0
Kotlinprogramming~10 mins

It keyword for single parameter in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - It keyword for single parameter
Start Lambda
↓
Single Parameter?
Yes↓
Use 'it' as parameter
↓
Execute Lambda Body
↓
Return Result
↓
End
When a lambda has one parameter, Kotlin lets you use 'it' to refer to it without naming it explicitly.
Execution Sample
Kotlin
val numbers = listOf(1, 2, 3)
val doubled = numbers.map { it * 2 }
println(doubled)
This code doubles each number in the list using 'it' as the single lambda parameter.
Execution Table
StepLambda Input (it)OperationResultOutput List State
111 * 22[2]
222 * 24[2, 4]
333 * 26[2, 4, 6]
4-End of list-[2, 4, 6]
💡 All elements processed, lambda applied to each using 'it', resulting list created.
Variable Tracker
VariableStartAfter 1After 2After 3Final
it-123-
doubled[][2][2, 4][2, 4, 6][2, 4, 6]
Key Moments - 3 Insights
Why can we use 'it' without declaring a parameter name?
Because the lambda has exactly one parameter, Kotlin automatically provides 'it' as its name (see execution_table steps 1-3).
What if the lambda had more than one parameter?
Then you must name the parameters explicitly; 'it' only works for single-parameter lambdas (not shown in this example).
Is 'it' a keyword or a variable?
'it' is an implicit name Kotlin gives to the single parameter inside the lambda, acting like a variable you can use directly.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'it' at step 2?
A2
B1
C3
D4
💡 Hint
Check the 'Lambda Input (it)' column at step 2 in the execution_table.
At which step does the output list first contain [2, 4]?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Output List State' column in the execution_table.
If the lambda had two parameters, could we still use 'it'?
AYes, 'it' always works
BNo, 'it' only works for one parameter
COnly if we rename 'it'
DOnly if we use a different keyword
💡 Hint
Refer to the second key_moment about multiple parameters.
Concept Snapshot
Kotlin lambdas with one parameter can use 'it' implicitly.
No need to name the parameter explicitly.
Use 'it' inside the lambda body to refer to the parameter.
Example: list.map { it * 2 } doubles each element.
If more than one parameter, 'it' cannot be used.
Full Transcript
This example shows how Kotlin lets you use 'it' as a shortcut for the single parameter in a lambda. The code doubles each number in a list by multiplying 'it' by 2. The execution table traces each step: the input value 'it', the operation, the result, and how the output list grows. Beginners often wonder why 'it' works without declaration; it's because Kotlin automatically names the single parameter 'it'. If there were more parameters, you would have to name them explicitly. This visual helps you see how 'it' changes with each element and how the final list is built.

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

  1. Step 1: Understand lambda parameters

    In Kotlin, if a lambda has only one parameter, you don't need to name it explicitly.
  2. Step 2: Role of it

    The keyword it automatically refers to that single parameter inside the lambda.
  3. Final Answer:

    The single implicit parameter of the lambda -> Option A
  4. 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

  1. Step 1: Identify lambda with single parameter

    The lambda inside map has one parameter, so it can be used implicitly.
  2. Step 2: Check syntax correctness

    list.map { it * 2 } correctly uses it without naming the parameter explicitly.
  3. Final Answer:

    list.map { it * 2 } -> Option A
  4. 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

  1. Step 1: Understand the filter condition

    The lambda filters numbers where it > 1, so only numbers greater than 1 remain.
  2. Step 2: Apply filter to the list

    From the list [1, 2, 3], numbers 2 and 3 satisfy the condition.
  3. Final Answer:

    [2, 3] -> Option C
  4. 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

  1. Step 1: Analyze the lambda expression

    The expression it * is incomplete; it lacks a value after the multiplication operator.
  2. Step 2: Identify syntax error

    This causes a syntax error because Kotlin expects a complete expression after *.
  3. Final Answer:

    Syntax error: incomplete expression after * -> Option D
  4. 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

  1. Step 1: Understand the goal

    We want words whose lengths are greater than 3, so we filter words based on their length.
  2. 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.
  3. Final Answer:

    words.filter { it.length > 3 } -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard