0
0
Kotlinprogramming~20 mins

Also function behavior and use cases in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Also Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using also?
Consider the following Kotlin code snippet using the also function. What will be printed when this code runs?
Kotlin
val result = mutableListOf<Int>().also {
    it.add(1)
    it.add(2)
    it.add(3)
}
println(result)
A[1, 2, 3]
B[]
Cnull
DCompilation error
Attempts:
2 left
💡 Hint
Remember that also returns the original object after applying the block.
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of the also function in Kotlin?
Choose the best description of what the also function is used for in Kotlin.
ATo filter elements from a collection based on a condition.
BTo perform additional actions on an object and return the original object.
CTo transform an object and return the transformed result.
DTo create a new object by copying an existing one.
Attempts:
2 left
💡 Hint
Think about whether also changes the object or just lets you do something with it.
🔧 Debug
advanced
2:30remaining
Why does this Kotlin code using also cause a NullPointerException?
Examine the code below. Why does it throw a NullPointerException at runtime?
Kotlin
var name: String? = null
val result = name.also {
    println(it!!.length)
}
println(result)
ABecause <code>also</code> returns null and println cannot print null.
BBecause <code>println</code> cannot be called inside <code>also</code>.
CBecause <code>also</code> cannot be called on nullable types.
DBecause <code>it!!</code> forces a non-null assertion on a null value causing the exception.
Attempts:
2 left
💡 Hint
Check what it!! means when it is null.
🚀 Application
advanced
2:00remaining
How to use also to log and return an object in Kotlin?
You want to log the value of a variable while returning it unchanged in a Kotlin function. Which code snippet correctly uses also for this purpose?
Aval x = compute().also { println("Value is $it") }
Bval x = compute().apply { println("Value is $this") }
Cval x = compute().run { println("Value is $this") }
Dval x = compute().let { println("Value is $it") }
Attempts:
2 left
💡 Hint
also returns the original object after running the block with it.
Predict Output
expert
3:00remaining
What is the output of this Kotlin code combining also and apply?
Analyze the code below. What will be printed when it runs?
Kotlin
data class Person(var name: String, var age: Int)

val person = Person("Alice", 25).also {
    println("Also: ${it.name}")
}.apply {
    age += 1
    println("Apply: $age")
}
println("Final: ${person.age}")
A
Also: Alice
Apply: 26
Final: 25
B
Also: Alice
Apply: 25
Final: 25
C
Also: Alice
Apply: 26
Final: 26
D
Also: Alice
Apply: 25
Final: 26
Attempts:
2 left
💡 Hint
Remember that apply runs with this and returns the object, also runs with it and returns the object.