Bird
Raised Fist0
Kotlinprogramming~20 mins

This vs it receiver difference in Kotlin - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of This vs It in Kotlin
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of 'this' vs 'it' in Kotlin lambda
What is the output of the following Kotlin code snippet?
Kotlin
class Example {
    fun printThis() {
        val numbers = listOf(1, 2, 3)
        numbers.forEach {
            println("it: $it")
        }
        numbers.forEach { number ->
            println("number: $number")
        }
    }
}

fun main() {
    val example = Example()
    example.printThis()
}
A
it: 1
it: 2
it: 3
number: 1
number: 2
number: 3
B
this: 1
this: 2
this: 3
number: 1
number: 2
number: 3
C
it: 1
it: 2
it: 3
this: 1
this: 2
this: 3
D
this: 1
this: 2
this: 3
this: 1
this: 2
this: 3
Attempts:
2 left
💡 Hint
Remember that 'it' is the implicit name of the single parameter in a lambda when no explicit name is given.
🧠 Conceptual
intermediate
1:30remaining
Difference between 'this' and 'it' in Kotlin lambdas
Which statement correctly describes the difference between 'this' and 'it' inside a Kotlin lambda expression?
A'it' refers to the enclosing class instance, while 'this' refers to the lambda's parameter.
B'this' and 'it' both refer to the lambda's single parameter but in different contexts.
C'this' refers to the lambda's receiver object, while 'it' refers to the lambda's single parameter.
D'this' is always null inside lambdas, and 'it' is the lambda's receiver.
Attempts:
2 left
💡 Hint
Think about what 'this' usually means in Kotlin and what 'it' is used for in lambdas.
❓ Predict Output
advanced
2:30remaining
Output of nested lambdas using 'this' and 'it'
What is the output of this Kotlin code?
Kotlin
class Outer {
    val name = "Outer"
    fun run() {
        val inner = Inner()
        inner.action {
            println(this.name)
            println(it)
        }
    }
    inner class Inner {
        val name = "Inner"
        fun action(block: Inner.(String) -> Unit) {
            this.block("Parameter")
        }
    }
}

fun main() {
    val outer = Outer()
    outer.run()
}
A
Outer
this
B
Outer
Parameter
C
Inner
this
D
Inner
Parameter
Attempts:
2 left
💡 Hint
Remember that 'this' inside the lambda with receiver refers to the receiver object, and 'it' is the lambda's parameter.
🔧 Debug
advanced
1:30remaining
Identify the error in 'this' vs 'it' usage
What error will this Kotlin code produce?
Kotlin
fun main() {
    val list = listOf(1, 2, 3)
    list.forEach {
        println(this)
    }
}
AError: Unresolved reference: this
BPrints the list elements
CError: 'this' cannot be used inside lambda
DPrints the list object reference
Attempts:
2 left
💡 Hint
Consider what 'this' refers to inside a lambda without a receiver.
🚀 Application
expert
2:30remaining
Using 'this' as receiver and 'it' as parameter in Kotlin
Given the following Kotlin code, what will be the output?
Kotlin
class Greeter(val greeting: String) {
    fun greet() {
        val printGreeting: Greeter.(String) -> Unit = {
            println("$greeting, $it!")
        }
        this.printGreeting("World")
    }
}

fun main() {
    val greeter = Greeter("Hello")
    greeter.greet()
}
AWorld, Hello!
BHello, World!
CHello, it!
DError: Cannot access 'it' in this context
Attempts:
2 left
💡 Hint
Remember that in a lambda with receiver, 'this' refers to the receiver object and 'it' is the single parameter.

Practice

(1/5)
1. In Kotlin, what does this refer to inside a class method?
easy
A. The current instance of the class
B. The first parameter of a lambda
C. A global variable
D. A static method

Solution

  1. Step 1: Understand this in class context

    Inside a class method, this refers to the current object instance that called the method.
  2. Step 2: Differentiate from lambda parameter

    this is not a lambda parameter; that role is usually played by it in single-parameter lambdas.
  3. Final Answer:

    The current instance of the class -> Option A
  4. Quick Check:

    this in class = current object [OK]
Hint: Remember: this means current object in classes [OK]
Common Mistakes:
  • Confusing this with lambda parameter it
  • Thinking this is a global variable
  • Assuming this refers to static context
2. Which of the following is the correct way to refer to the implicit lambda parameter in Kotlin when there is only one parameter?
easy
A. this
B. it
C. self
D. lambda

Solution

  1. Step 1: Identify default lambda parameter

    In Kotlin, when a lambda has a single parameter and no explicit name, it is referred to as it.
  2. Step 2: Confirm this is not used here

    this refers to the receiver object, not the lambda parameter.
  3. Final Answer:

    it -> Option B
  4. Quick Check:

    Single lambda parameter = it [OK]
Hint: Single lambda parameter? Use it [OK]
Common Mistakes:
  • Using this instead of it in lambdas
  • Trying to name the parameter without declaration
  • Assuming self is valid in Kotlin
3. What will be the output of the following Kotlin code?
class Box(val value: Int) {
    fun printValue() {
        listOf(1).forEach {
            println(this.value + it)
        }
    }
}

fun main() {
    Box(5).printValue()
}
medium
A. 6
B. 1
C. 5
D. Compilation error

Solution

  1. Step 1: Understand this inside lambda with receiver

    Inside printValue, this refers to the Box instance, so this.value is 5.
  2. Step 2: Calculate the sum inside lambda

    The lambda parameter it is 1 from the list. So, this.value + it = 5 + 1 = 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    this.value + it = 6 [OK]
Hint: Inside lambda, this is outer object, it is lambda param [OK]
Common Mistakes:
  • Thinking this refers to lambda receiver
  • Confusing it with this
  • Expecting compilation error due to this usage
4. Identify the error in the following Kotlin code snippet:
fun main() {
    val numbers = listOf(1, 2, 3)
    numbers.forEach {
        println(this + it)
    }
}
medium
A. Missing lambda parameter name
B. Incorrect list declaration
C. Using this inside lambda without receiver causes error
D. No error, code runs fine

Solution

  1. Step 1: Check this usage inside lambda

    In this lambda, there is no receiver object, so this is not defined and causes an error.
  2. Step 2: Confirm lambda parameter usage

    it is valid as the lambda parameter, but this cannot be used without a receiver context.
  3. Final Answer:

    Using this inside lambda without receiver causes error -> Option C
  4. Quick Check:

    this needs receiver, else error [OK]
Hint: No receiver? Don't use this inside lambda [OK]
Common Mistakes:
  • Assuming this always refers to lambda parameter
  • Ignoring that this requires receiver context
  • Thinking code runs without error
5. Given the following Kotlin code, what will be printed?
class Calculator {
    fun calculate() {
        val numbers = listOf(10, 20)
        numbers.run {
            println(this.sum())
            numbers.forEach {
                println(it + this.sum())
            }
        }
    }
}

fun main() {
    Calculator().calculate()
}
hard
A. 10\n20\n30
B. Compilation error due to ambiguous this
C. 30\n30\n30
D. 30\n40\n50

Solution

  1. Step 1: Understand this inside run block

    Inside run, this refers to the numbers list. So, this.sum() is 30.
  2. Step 2: Calculate outputs inside forEach

    For each it in numbers (10 and 20), print it + this.sum(): 10+30=40 and 20+30=50.
  3. Step 3: List all printed lines

    First print: 30
    Then prints: 40 and 50
  4. Final Answer:

    30 40 50 -> Option D
  5. Quick Check:

    this in run = list, sum=30 [OK]
Hint: Inside run, this is receiver object [OK]
Common Mistakes:
  • Confusing this inside nested lambdas
  • Assuming this refers to Calculator instance
  • Expecting compilation error due to this
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