0
0
Kotlinprogramming~20 mins

Lazy evaluation vs eager evaluation in Kotlin - Practice Questions

Choose your learning style9 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.