Bird
Raised Fist0
Kotlinprogramming~20 mins

Lazy evaluation vs eager evaluation in Kotlin - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Lazy Evaluation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of eager vs lazy list processing
Consider the following Kotlin code that processes a list eagerly and lazily. What is the output when running this code?
Kotlin
fun main() {
    val numbers = listOf(1, 2, 3, 4)
    val eager = numbers.map { println("Eager: $it"); it * 2 }
    val lazy = numbers.asSequence().map { println("Lazy: $it"); it * 2 }
    println("Eager result: $eager")
    println("Lazy result: ${lazy.toList()}")
}
A
Eager result: [2, 4, 6, 8]
Lazy result: [2, 4, 6, 8]
Eager: 1
Eager: 2
Eager: 3
Eager: 4
Lazy: 1
Lazy: 2
Lazy: 3
Lazy: 4
B
Eager: 1
Eager: 2
Eager: 3
Eager: 4
Eager result: [2, 4, 6, 8]
Lazy: 1
Lazy: 2
Lazy: 3
Lazy: 4
Lazy result: [2, 4, 6, 8]
C
Lazy: 1
Lazy: 2
Lazy: 3
Lazy: 4
Lazy result: [2, 4, 6, 8]
Eager: 1
Eager: 2
Eager: 3
Eager: 4
Eager result: [2, 4, 6, 8]
D
Eager: 1
Eager: 2
Eager: 3
Eager: 4
Lazy result: [2, 4, 6, 8]
Eager result: [2, 4, 6, 8]
Lazy: 1
Lazy: 2
Lazy: 3
Lazy: 4
Attempts:
2 left
💡 Hint
Remember that eager operations run immediately, while lazy operations run when the result is requested.
❓ Predict Output
intermediate
2:00remaining
Effect of filtering on lazy vs eager collections
What is the output of this Kotlin code that filters a list eagerly and lazily?
Kotlin
fun main() {
    val numbers = listOf(1, 2, 3, 4)
    val eager = numbers.filter { println("Eager filter: $it"); it % 2 == 0 }
    val lazy = numbers.asSequence().filter { println("Lazy filter: $it"); it % 2 == 0 }
    println("Eager filtered: $eager")
    println("Lazy filtered: ${lazy.toList()}")
}
A
Eager filter: 1
Eager filter: 2
Eager filter: 3
Eager filter: 4
Eager filtered: [2, 4]
Lazy filter: 1
Lazy filter: 2
Lazy filter: 3
Lazy filter: 4
Lazy filtered: [2, 4]
B
Lazy filter: 1
Lazy filter: 2
Lazy filter: 3
Lazy filter: 4
Lazy filtered: [2, 4]
Eager filter: 1
Eager filter: 2
Eager filter: 3
Eager filter: 4
Eager filtered: [2, 4]
C
Eager filtered: [2, 4]
Lazy filtered: [2, 4]
Eager filter: 1
Eager filter: 2
Eager filter: 3
Eager filter: 4
Lazy filter: 1
Lazy filter: 2
Lazy filter: 3
Lazy filter: 4
D
Eager filter: 1
Eager filter: 2
Eager filter: 3
Eager filter: 4
Lazy filtered: [2, 4]
Eager filtered: [2, 4]
Lazy filter: 1
Lazy filter: 2
Lazy filter: 3
Lazy filter: 4
Attempts:
2 left
💡 Hint
Filtering eagerly runs the predicate immediately, lazy filtering runs it when the result is requested.
🔧 Debug
advanced
2:00remaining
Why does this lazy sequence not print anything?
This Kotlin code uses a lazy sequence with map and filter, but nothing is printed. Why?
Kotlin
fun main() {
    val numbers = listOf(1, 2, 3, 4)
    val lazy = numbers.asSequence()
        .map { println("Mapping: $it"); it * 2 }
        .filter { println("Filtering: $it"); it > 4 }
    // No terminal operation here
}
ABecause the list is empty, so map and filter have no elements to process.
BBecause the println statements are inside lambdas and lambdas never run in Kotlin.
CBecause the sequence operations are lazy and no terminal operation is called, so map and filter are never executed.
DBecause the filter condition is false for all elements, so nothing is printed.
Attempts:
2 left
💡 Hint
Think about when lazy sequences actually run their operations.
❓ Predict Output
advanced
2:00remaining
Output of mixed eager and lazy operations
What is the output of this Kotlin code mixing eager and lazy operations?
Kotlin
fun main() {
    val numbers = listOf(1, 2, 3)
    val eager = numbers.map { println("Eager map: $it"); it + 1 }
    val lazy = numbers.asSequence().map { println("Lazy map: $it"); it + 1 }
    println("Eager sum: ${eager.sum()}")
    println("Lazy sum: ${lazy.sum()}")
}
A
Lazy map: 1
Lazy map: 2
Lazy map: 3
Lazy sum: 9
Eager map: 1
Eager map: 2
Eager map: 3
Eager sum: 9
B
Eager sum: 9
Lazy sum: 9
Eager map: 1
Eager map: 2
Eager map: 3
Lazy map: 1
Lazy map: 2
Lazy map: 3
C
Eager map: 1
Eager map: 2
Eager map: 3
Lazy sum: 9
Eager sum: 9
Lazy map: 1
Lazy map: 2
Lazy map: 3
D
Eager map: 1
Eager map: 2
Eager map: 3
Eager sum: 9
Lazy map: 1
Lazy map: 2
Lazy map: 3
Lazy sum: 9
Attempts:
2 left
💡 Hint
Eager map runs immediately, lazy map runs when sum() is called.
🧠 Conceptual
expert
2:00remaining
Why prefer lazy evaluation in large data processing?
Which of the following is the best reason to prefer lazy evaluation over eager evaluation when processing large data sets in Kotlin?
ALazy evaluation reduces memory usage by processing elements only when needed, avoiding creating large intermediate collections.
BLazy evaluation always runs faster because it uses multiple threads automatically.
CLazy evaluation guarantees that all elements are processed in parallel for better performance.
DLazy evaluation stores all intermediate results in memory to speed up repeated access.
Attempts:
2 left
💡 Hint
Think about how lazy evaluation handles data compared to eager evaluation.

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

  1. Step 1: Understand lazy evaluation

    Lazy evaluation means the program waits to calculate a value until it is actually needed.
  2. Step 2: Understand eager evaluation

    Eager evaluation means the program calculates the value right away when the code runs.
  3. Final Answer:

    Lazy evaluation delays computation until the value is needed, eager evaluation computes immediately. -> Option C
  4. Quick Check:

    Lazy = delay, Eager = immediate [OK]
Hint: Lazy waits, eager runs now. Remember delay vs immediate [OK]
Common Mistakes:
  • Confusing which evaluation runs first
  • Thinking lazy evaluation runs immediately
  • Assuming eager evaluation delays work
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

  1. Step 1: Recall Kotlin lazy syntax

    Kotlin uses val x by lazy { ... } to declare lazy properties.
  2. Step 2: Check each option

    val x by lazy { 10 } matches the correct syntax. The other options are incorrect Kotlin syntax.
  3. Final Answer:

    val x by lazy { 10 } -> Option A
  4. 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

  1. Step 1: Understand eager initialization

    The variable eager uses also, so "Eager initialized" prints immediately when declared.
  2. 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.
  3. Final Answer:

    Eager initialized Start main Eager Lazy initialized Lazy Lazy -> Option B
  4. 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

  1. Step 1: Check property mutability

    The property lazyValue is declared with val, meaning it cannot be reassigned after initialization.
  2. 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.
  3. Final Answer:

    Cannot reassign a val property declared with 'by lazy'. -> Option A
  4. 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

  1. Step 1: Understand lazy loading benefit

    Loading a large config only when needed saves startup time and resources.
  2. Step 2: Choose lazy evaluation syntax

    Using val config by lazy { loadConfig() } delays loading until first use, improving performance.
  3. Final Answer:

    Declare the configuration as val config by lazy { loadConfig() } so it loads only on first access. -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard