0
0
Kotlinprogramming~5 mins

Lazy property delegation in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Aval x by lazy { ... }
Bval x = lazy { ... }
Clazy val x = ...
Dval x = lazy()
When is the value of a lazy property computed?
ANever
BWhen the object is created
CEvery time the property is accessed
DWhen the property is first accessed
What happens if multiple threads access a lazy property by default?
AThe property is computed multiple times
BThe property is computed once safely
CAn error occurs
DThe property is not computed
Which of these is NOT a benefit of lazy property delegation?
AAlways recomputes value on access
BImproves startup performance
CDelays expensive computation
DCaches computed value
How can you change the thread-safety mode of a lazy property?
ABy overriding a method
BBy using a different keyword
CBy passing a parameter to lazy()
DYou cannot change it
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.