0
0
Kotlinprogramming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
With 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 with function?
Consider the following Kotlin code snippet using the with 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", 30)
    val result = with(person) {
        age += 1
        "${name} is now $age years old"
    }
    println(result)
}
ACompilation error due to string interpolation
BAlice is now 30 years old
CAlice is now 31 years old
DRuntime error: Unresolved reference
Attempts:
2 left
💡 Hint
Remember that with allows you to access the object's members directly and the last expression is returned.
🧠 Conceptual
intermediate
1:30remaining
What is the main difference between with and apply in Kotlin?
Choose the option that correctly describes the key difference between with and apply functions in Kotlin.
A<code>with</code> returns the lambda result, <code>apply</code> returns the receiver object
B<code>with</code> returns the receiver object, <code>apply</code> returns the lambda result
CBoth <code>with</code> and <code>apply</code> return the receiver object
DBoth <code>with</code> and <code>apply</code> return the lambda result
Attempts:
2 left
💡 Hint
Think about what each function returns after executing the block.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin code using with function cause a compilation error?
Examine the code below. It tries to use with on a nullable String. Why does it fail to compile?
Kotlin
fun main() {
    val text: String? = null
    with(text) {
        println(length)
    }
}
AThe println statement has a syntax error
BCannot call with on a nullable receiver without safe call operator
CThe length property does not exist on String
Dwith requires a lambda with receiver, but none is provided
Attempts:
2 left
💡 Hint
Consider how Kotlin handles nullable types and scope functions.
Predict Output
advanced
2:00remaining
What is the output of this Kotlin code using nested with functions?
Analyze the following Kotlin code with nested with calls. What will it print?
Kotlin
data class Address(var city: String, var zip: String)

data class User(var name: String, var address: Address)

fun main() {
    val user = User("Bob", Address("NYC", "10001"))
    val result = with(user) {
        with(address) {
            "${this@with.name} lives in $city with zip $zip"
        }
    }
    println(result)
}
ARuntime error: Unresolved reference to name
BCompilation error due to nested with
CBob lives in with zip 10001
DBob lives in NYC with zip 10001
Attempts:
2 left
💡 Hint
Remember that nested with blocks can access outer variables if referenced properly.
🧠 Conceptual
expert
1:30remaining
Which statement about Kotlin's with function is TRUE?
Select the correct statement about the behavior and use cases of Kotlin's with function.
A<code>with</code> can be used to execute multiple operations on an object and returns the last expression result, but it is not an extension function.
B<code>with</code> is an extension function on any object and returns the object itself after executing the block.
C<code>with</code> requires the receiver object to be nullable and safely unwraps it inside the block.
D<code>with</code> is used to create new objects and always returns a new instance.
Attempts:
2 left
💡 Hint
Think about the signature and return type of with.