Challenge - 5 Problems
Lazy Property Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Remember that the lazy block runs only once, when the property is accessed the first time.
✗ Incorrect
The lazy block prints "Computed!" only once when lazyValue is accessed the first time. The first println prints the message before accessing lazyValue. Then accessing lazyValue triggers the lazy block, printing "Computed!" and returning "Hello". The second access just returns "Hello" without recomputing.
🧠 Conceptual
intermediate2: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?
Attempts:
2 left
💡 Hint
Check the default thread safety mode of Kotlin's lazy delegate.
✗ Incorrect
By default, Kotlin's lazy delegate uses SYNCHRONIZED mode, which ensures the lazy block is executed only once safely even with multiple threads.
❓ Predict Output
advanced2: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) }
Attempts:
2 left
💡 Hint
LazyThreadSafetyMode.NONE disables synchronization but does not change the single execution behavior in single-threaded code.
✗ Incorrect
With LazyThreadSafetyMode.NONE, the lazy block runs once when first accessed. It prints "Calculating..." once and returns "Done" twice.
🔧 Debug
advanced2: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) }
Attempts:
2 left
💡 Hint
Look at the expression inside the lazy block carefully.
✗ Incorrect
The expression null!! throws a NullPointerException immediately when the lazy block runs. This causes the exception on first access.
🚀 Application
expert2: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) }
Attempts:
2 left
💡 Hint
The lazy initializer runs only once regardless of how many times the property is accessed.
✗ Incorrect
The lazy block runs once on the first access, incrementing count once. Subsequent accesses do not run the block again.