Eager evaluation means the program calculates values immediately when the code runs. It’s like buying groceries right away when you think of them.
Click to reveal answer
beginner
What is lazy evaluation in Kotlin?
Lazy evaluation delays calculating a value until it is actually needed. It’s like waiting to buy groceries until you open the fridge and see you need them.
Click to reveal answer
beginner
How do you create a lazy property in Kotlin?
Use the keyword by lazy. For example: val name by lazy { "Kotlin" } means name is calculated only when first used.
Click to reveal answer
intermediate
What is a benefit of lazy evaluation?
It saves time and resources by not doing work until necessary. This can make programs faster and use less memory.
Click to reveal answer
intermediate
Give an example where eager evaluation might be better than lazy evaluation.
When you need a value immediately and want to avoid delays later, eager evaluation is better. For example, reading user input right away.
Click to reveal answer
In Kotlin, which keyword is used for lazy evaluation?
Aval
Blateinit
Cfun
Dby lazy
✗ Incorrect
The keyword by lazy creates a property that is evaluated only when first accessed.
What happens in eager evaluation?
AValues are calculated only when needed
BValues are never calculated
CValues are calculated immediately
DValues are calculated randomly
✗ Incorrect
Eager evaluation means the program calculates values right away.
Which is a benefit of lazy evaluation?
ASaves resources by delaying work
BAlways faster than eager evaluation
CCalculates all values upfront
DPrevents any calculation
✗ Incorrect
Lazy evaluation saves time and memory by delaying calculations until needed.
Which Kotlin property is NOT lazy by default?
Aval without lazy
Bval by lazy
Cvar by lazy
Dvar lateinit
✗ Incorrect
A normal val is eager and calculated immediately.
When might eager evaluation be preferred?
AWhen you want to save memory
BWhen you need the value immediately
CWhen you never use the value
DWhen you want to delay work
✗ Incorrect
Eager evaluation is best when the value is needed right away.
Explain the difference between lazy evaluation and eager evaluation in Kotlin.
Think about when the value is calculated.
You got /3 concepts.
Describe a situation where lazy evaluation improves program performance.
Consider saving resources by not doing unnecessary work.
You got /3 concepts.
Practice
(1/5)
1. What is the main difference between lazy evaluation and eager evaluation in Kotlin?
easy
A. Lazy evaluation requires manual calls, eager evaluation never runs automatically.
B. Lazy evaluation computes values immediately, eager evaluation delays computation.
C. Lazy evaluation delays computation until the value is needed, eager evaluation computes immediately.
D. Lazy evaluation is only used for variables, eager evaluation only for functions.
Solution
Step 1: Understand lazy evaluation
Lazy evaluation means the program waits to calculate a value until it is actually needed.
Step 2: Understand eager evaluation
Eager evaluation means the program calculates the value right away when the code runs.
Final Answer:
Lazy evaluation delays computation until the value is needed, eager evaluation computes immediately. -> Option C
2. Which of the following is the correct syntax to declare a lazy property in Kotlin?
easy
A. val x by lazy { 10 }
B. val x = lazy { 10 }
C. lazy val x = 10
D. val x = by lazy 10
Solution
Step 1: Recall Kotlin lazy syntax
Kotlin uses val x by lazy { ... } to declare lazy properties.
Step 2: Check each option
val x by lazy { 10 } matches the correct syntax. The other options are incorrect Kotlin syntax.
Final Answer:
val x by lazy { 10 } -> Option A
Quick Check:
Use 'by lazy { }' for lazy properties [OK]
Hint: Remember: 'val x by lazy { ... }' is correct syntax [OK]
Common Mistakes:
Using 'lazy val' instead of 'val by lazy'
Assigning lazy without 'by'
Placing 'by lazy' after the value
3. What will be the output of the following Kotlin code?
val eager = "Eager".also { println("Eager initialized") }
val lazyVal by lazy { println("Lazy initialized"); "Lazy" }
fun main() {
println("Start main")
println(eager)
println(lazyVal)
println(lazyVal)
}
medium
A. Start main
Eager initialized
Eager
Lazy initialized
Lazy
Lazy
B. Eager initialized
Start main
Eager
Lazy initialized
Lazy
Lazy
C. Eager initialized
Start main
Eager
Lazy
Lazy initialized
Lazy
D. Start main
Eager
Lazy initialized
Lazy
Lazy initialized
Lazy
Solution
Step 1: Understand eager initialization
The variable eager uses also, so "Eager initialized" prints immediately when declared.
Step 2: Understand lazy initialization and main flow
Lazy variable lazyVal prints "Lazy initialized" only when first accessed. The main prints "Start main", then prints eager (already initialized), then accesses lazyVal twice. The first access triggers initialization print, second does not.
Final Answer:
Eager initialized
Start main
Eager
Lazy initialized
Lazy
Lazy -> Option B
Quick Check:
Eager prints first, lazy prints on first access [OK]
Hint: Eager prints on declaration, lazy prints on first use [OK]
Common Mistakes:
Assuming lazy prints before main
Thinking lazy prints every access
Mixing order of prints
4. Identify the error in this Kotlin code using lazy evaluation:
val lazyValue by lazy {
println("Calculating...")
42
}
fun main() {
println(lazyValue)
lazyValue = 50
}
medium
A. Cannot reassign a val property declared with 'by lazy'.
B. Missing parentheses after 'lazy' keyword.
C. Lazy block must return a String, not Int.
D. The println statement inside lazy block is not allowed.
Solution
Step 1: Check property mutability
The property lazyValue is declared with val, meaning it cannot be reassigned after initialization.
Step 2: Analyze reassignment in main
The code tries to assign lazyValue = 50, which is illegal for a val property and causes a compilation error.
Final Answer:
Cannot reassign a val property declared with 'by lazy'. -> Option A
Quick Check:
val lazy properties are read-only, no reassignment [OK]
Hint: val lazy properties cannot be reassigned after initialization [OK]
Common Mistakes:
Trying to assign a new value to a val lazy property
Confusing val with var for lazy properties
Thinking lazy block must return String
5. You want to optimize a Kotlin program that loads a large configuration file only if needed. Which approach best uses lazy evaluation to improve performance?
hard
A. Use a var and assign loadConfig() inside main after checking a flag.
B. Call loadConfig() immediately at program start and store in a val.
C. Declare val config = loadConfig() inside a function that runs every time.
D. Declare the configuration as val config by lazy { loadConfig() } so it loads only on first access.
Solution
Step 1: Understand lazy loading benefit
Loading a large config only when needed saves startup time and resources.
Step 2: Choose lazy evaluation syntax
Using val config by lazy { loadConfig() } delays loading until first use, improving performance.
Final Answer:
Declare the configuration as val config by lazy { loadConfig() } so it loads only on first access. -> Option D
Quick Check:
Lazy loading delays heavy work until needed [OK]
Hint: Use 'by lazy' to delay heavy loading until first use [OK]
Common Mistakes:
Loading config immediately even if not needed
Using var without lazy, causing unnecessary loads
Loading config repeatedly inside functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.