Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
In Kotlin, lazy is used to create a lazily evaluated property.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
toList() creates a new list eagerly from the original collection.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toList' which evaluates eagerly.
Applying 'map' directly on list which is eager.
✗ Incorrect
asSequence() converts the list to a lazy sequence, delaying computation.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' or 'filter' in wrong places.
Not converting back to list after lazy operations.
✗ Incorrect
First, asSequence() creates a lazy sequence, then toList() collects results eagerly.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using eager initialization instead of lazy.
Mixing up string methods or forgetting parentheses.
✗ Incorrect
The property is lazy initialized with lazy. Then we print its length and uppercase form.