0
0
Kotlinprogramming~10 mins

Why sequences matter for performance in Kotlin - Test Your Understanding

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

Complete the code to create a list of numbers from 1 to 5.

Kotlin
val numbers = listOf([1])
Drag options to blanks, or click blank then click option'
AlistOf(1, 2, 3, 4, 5)
B1..5
C1, 2, 3, 4, 5
DarrayOf(1, 2, 3, 4, 5)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range like 1..5 inside listOf creates a list with one element (the range), not the numbers.
Using arrayOf instead of listOf creates an array, not a list.
2fill in blank
medium

Complete the code to convert a list to a sequence.

Kotlin
val seq = numbers.[1]()
Drag options to blanks, or click blank then click option'
AasSequence
BtoSet
CtoList
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using toList() returns a list, not a sequence.
Using map() requires a lambda and does not convert to sequence.
3fill in blank
hard

Fix the error in the code to filter even numbers using a sequence.

Kotlin
val evens = numbers.asSequence().filter { it [1] 2 == 0 }.toList()
Drag options to blanks, or click blank then click option'
A%
B/
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using division / instead of modulo %.
Using multiplication or addition operators which do not check divisibility.
4fill in blank
hard

Fill both blanks to create a sequence that maps numbers to their squares and then converts to a list.

Kotlin
val squares = numbers.[1]().[2] { it * it }.toList()
Drag options to blanks, or click blank then click option'
AasSequence
Bmap
Cfilter
DtoList
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter instead of map to transform elements.
Using toList too early, before mapping.
5fill in blank
hard

Fill all three blanks to create a sequence that filters odd numbers, maps to double their value, and converts to a list.

Kotlin
val result = numbers.[1]().[2] { it % 2 != 0 }.[3] { it * 2 }.toList()
Drag options to blanks, or click blank then click option'
AasSequence
Bfilter
Cmap
DtoList
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up filter and map order.
Not converting to sequence before filtering and mapping.