Recall & Review
beginner
What is lazy property delegation in Kotlin?
Lazy property delegation means the property value is computed only once when it is first accessed, not when the object is created. This helps save resources by delaying work until needed.
Click to reveal answer
beginner
How do you declare a lazy property in Kotlin?
Use the
by lazy { ... } syntax. For example: <br>val name: String by lazy { "John" } means name is computed only on first use.Click to reveal answer
beginner
What happens if you access a lazy property multiple times?
The lazy property is computed only once on the first access. Later accesses return the cached value without recomputing.
Click to reveal answer
intermediate
Can lazy properties be thread-safe in Kotlin?
Yes. By default,
lazy is thread-safe and ensures the value is computed only once even in multiple threads. You can change this behavior with parameters.Click to reveal answer
intermediate
Why use lazy property delegation instead of initializing directly?
Lazy delegation delays expensive computations or resource loading until needed. This improves performance and reduces startup time.
Click to reveal answer
How do you declare a lazy property in Kotlin?
✗ Incorrect
The correct syntax is
val x by lazy { ... } to delegate property initialization lazily.When is the value of a lazy property computed?
✗ Incorrect
Lazy properties compute their value only on the first access, not at object creation.
What happens if multiple threads access a lazy property by default?
✗ Incorrect
By default, lazy properties are thread-safe and computed only once even with multiple threads.
Which of these is NOT a benefit of lazy property delegation?
✗ Incorrect
Lazy properties do NOT recompute on every access; they cache the value after first computation.
How can you change the thread-safety mode of a lazy property?
✗ Incorrect
You can pass parameters like
LazyThreadSafetyMode.NONE to lazy() to change thread-safety.Explain how lazy property delegation works in Kotlin and why it is useful.
Think about when the property value is created and how it helps with resources.
You got /4 concepts.
Describe the syntax to declare a lazy property and how thread-safety is handled by default.
Focus on the keyword and default behavior in multi-threaded situations.
You got /4 concepts.