0
0
Kotlinprogramming~20 mins

Lazy property delegation in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lazy Property Master
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 lazy delegation?
Consider the following Kotlin code snippet. What will be printed when the program runs?
Kotlin
class Example {
    val lazyValue: String by lazy {
        println("Computed!")
        "Hello"
    }
}

fun main() {
    val example = Example()
    println("Before accessing lazyValue")
    println(example.lazyValue)
    println(example.lazyValue)
}
A
Before accessing lazyValue
Computed!
Hello
Hello
B
Computed!
Before accessing lazyValue
Hello
Hello
C
Before accessing lazyValue
Hello
Computed!
Hello
D
Before accessing lazyValue
Hello
Hello
Computed!
Attempts:
2 left
💡 Hint
Remember that the lazy block runs only once, when the property is accessed the first time.
🧠 Conceptual
intermediate
2:00remaining
What happens if you access a lazy property from multiple threads without synchronization?
In Kotlin, if a lazy property is declared with the default lazy() delegate and accessed from multiple threads simultaneously, what is the expected behavior?
AThe lazy block is executed exactly once in a thread-safe manner.
BThe lazy block may be executed multiple times concurrently, causing race conditions.
CThe program will throw a runtime exception due to concurrent access.
DThe lazy property will always return null if accessed from multiple threads.
Attempts:
2 left
💡 Hint
Check the default thread safety mode of Kotlin's lazy delegate.
Predict Output
advanced
2:00remaining
What is the output of this Kotlin code with custom lazy mode?
Analyze the following Kotlin code. What will be printed when main() runs?
Kotlin
class Test {
    val lazyValue: String by lazy(LazyThreadSafetyMode.NONE) {
        println("Calculating...")
        "Done"
    }
}

fun main() {
    val test = Test()
    println(test.lazyValue)
    println(test.lazyValue)
}
A
Done
Done
Done
B
Done
Calculating...
Done
C
Calculating...
Calculating...
Done
Done
D
Calculating...
Done
Done
Attempts:
2 left
💡 Hint
LazyThreadSafetyMode.NONE disables synchronization but does not change the single execution behavior in single-threaded code.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin lazy property cause a NullPointerException?
Examine the code below. Why does accessing lazyValue cause a NullPointerException?
Kotlin
class Demo {
    val lazyValue: String by lazy {
        null!!.toString()
    }
}

fun main() {
    val demo = Demo()
    println(demo.lazyValue)
}
AThe NullPointerException occurs because lazyValue is accessed before the Demo object is fully constructed.
BThe lazy property is not initialized properly because lazy requires a non-null initializer.
CThe lazy block throws a NullPointerException because null!! forces a null dereference.
DThe code compiles but prints "null" without exception.
Attempts:
2 left
💡 Hint
Look at the expression inside the lazy block carefully.
🚀 Application
expert
2:00remaining
How many times is the initializer called in this Kotlin lazy property example with multiple accesses?
Given the code below, how many times will the initializer block inside lazy be executed?
Kotlin
class Counter {
    var count = 0
    val lazyValue: String by lazy {
        count++
        "Counted"
    }
}

fun main() {
    val c = Counter()
    println(c.lazyValue)
    println(c.lazyValue)
    println(c.lazyValue)
    println(c.count)
}
A0
B1
C3
D4
Attempts:
2 left
💡 Hint
The lazy initializer runs only once regardless of how many times the property is accessed.