0
0
Kotlinprogramming~10 mins

Lazy evaluation vs eager evaluation in Kotlin - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a lazy value in Kotlin.

Kotlin
val lazyValue: String by [1] {
    println("Computed!")
    "Hello"
}
Drag options to blanks, or click blank then click option'
Aeager
Bvar
Cval
Dlazy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val' or 'var' instead of 'lazy' for lazy initialization.
Trying to use 'eager' which is not a Kotlin keyword.
2fill in blank
medium

Complete the code to create an eager list in Kotlin.

Kotlin
val numbers = listOf(1, 2, 3, 4).[1]()
Drag options to blanks, or click blank then click option'
AtoList
Bmap
Cfilter
DasSequence
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'asSequence' which creates a lazy sequence, not an eager list.
Using 'map' or 'filter' without collecting results eagerly.
3fill in blank
hard

Fix the error in the code to make the sequence evaluated lazily.

Kotlin
val seq = listOf(1, 2, 3).[1]().map { it * 2 }
Drag options to blanks, or click blank then click option'
AasSequence
BtoList
Cmap
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toList' which evaluates eagerly.
Applying 'map' directly on list which is eager.
4fill in blank
hard

Fill both blanks to create a lazy sequence and then convert it back to a list eagerly.

Kotlin
val result = listOf(1, 2, 3).[1]().map { it + 1 }.[2]()
Drag options to blanks, or click blank then click option'
AasSequence
BtoList
Cfilter
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' or 'filter' in wrong places.
Not converting back to list after lazy operations.
5fill in blank
hard

Fill all three blanks to create a lazy property, access it, and print its value.

Kotlin
val lazyVal: String by [1] {
    println("Computing...")
    "Kotlin"
}

fun main() {
    println(lazyVal)
    println(lazyVal.[2])
    println(lazyVal.[3])
}
Drag options to blanks, or click blank then click option'
Alazy
BtoUpperCase()
Clength
Dsubstring(0, 3)
Attempts:
3 left
💡 Hint
Common Mistakes
Using eager initialization instead of lazy.
Mixing up string methods or forgetting parentheses.