0
0
Kotlinprogramming~20 mins

This vs it receiver difference in Kotlin - Practice Questions

Choose your learning style9 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.