0
0
Kotlinprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Apply 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 apply?
Consider the following Kotlin code snippet using the apply function. What will be printed when this code runs?
Kotlin
data class Person(var name: String, var age: Int)

fun main() {
    val person = Person("Alice", 25).apply {
        name = "Bob"
        age += 5
    }
    println(person)
}
APerson(name=Bob, age=30)
BPerson(name=Alice, age=25)
CPerson(name=Bob, age=25)
DPerson(name=Alice, age=30)
Attempts:
2 left
💡 Hint
Remember that apply runs the block on the object and returns the object itself.
🧠 Conceptual
intermediate
1:30remaining
Which statement best describes the behavior of Kotlin's apply function?
Choose the statement that correctly describes how the apply function works in Kotlin.
A<code>apply</code> executes a block with the object as <code>this</code> and returns the object itself.
B<code>apply</code> executes a block with the object as an argument and returns the object itself.
C<code>apply</code> executes a block with the object as an argument and returns the result of the block.
D<code>apply</code> executes a block with the object as <code>this</code> and returns the result of the block.
Attempts:
2 left
💡 Hint
Think about what this refers to inside the block and what the function returns.
🔧 Debug
advanced
2:30remaining
Why does this Kotlin code throw a NullPointerException when using apply?
Examine the code below. Why does it throw a NullPointerException at runtime?
Kotlin
class Box(var content: String?)

fun main() {
    val box: Box? = Box(null)
    box?.apply {
        content = content!!.uppercase()
    }
    println(box?.content)
}
ABecause <code>apply</code> block is executed even if <code>box</code> is null.
BBecause <code>content!!</code> throws NullPointerException since <code>box</code> is null.
CBecause <code>content</code> is null and <code>!!</code> forces a non-null assertion causing the exception.
DBecause <code>apply</code> returns null when called on a null object.
Attempts:
2 left
💡 Hint
Check the use of the !! operator inside the apply block.
📝 Syntax
advanced
2:00remaining
Which Kotlin code snippet correctly uses apply to initialize a mutable list?
Select the code snippet that correctly uses apply to add elements to a mutable list and returns the list.
A
val list = mutableListOf&lt;Int&gt;().apply {
    this.add(1)
    this.add(2)
    this.add(3)
    return this
}
B
val list = mutableListOf&lt;Int&gt;().apply {
    add(1)
    add(2)
    add(3)
}
C
val list = mutableListOf&lt;Int&gt;().apply {
    add(1)
    add(2)
    add(3)
    return@apply this
}
D
val list = mutableListOf&lt;Int&gt;().apply {
    add(1)
    add(2)
    add(3)
    return
}
Attempts:
2 left
💡 Hint
Remember that apply returns the object itself automatically after the block.
🚀 Application
expert
3:00remaining
How many items are in the list after this Kotlin code runs?
Analyze the Kotlin code below. After execution, how many elements does the list contain?
Kotlin
val list = mutableListOf<String>().apply {
    add("a")
    add("b")
}.apply {
    add("c")
    remove("a")
}.apply {
    add("d")
    clear()
    add("e")
}

println(list.size)
A4
B2
C3
D1
Attempts:
2 left
💡 Hint
Follow each apply block step by step and track the list contents.