Consider the following Kotlin code that processes a list of numbers. What is the output of the code snippet below?
val numbers = listOf(1, 2, 3, 4, 5) val result = numbers.asSequence() .map { it * 2 } .filter { it > 5 } .toList() println(result)
Remember that asSequence() processes elements lazily and filters after mapping.
The sequence maps each number by multiplying by 2, then filters those greater than 5. So 6, 8, and 10 remain.
Which of the following best explains why you might choose a Sequence over a Collection in Kotlin?
Think about how data is processed step-by-step in sequences.
Sequences process elements lazily, meaning operations are done one by one without creating intermediate collections, which saves memory and can improve performance on large data.
Which option contains a syntax error when trying to create a sequence and filter elements greater than 10?
val seq = listOf(5, 10, 15, 20).asSequence() val filtered = seq.filter { it > 10 } println(filtered.toList())
Check the syntax of lambda expressions in Kotlin.
Option D is missing the lambda braces and parameter declaration, causing a syntax error.
You want to optimize the following Kotlin code for performance on a large list. Which option best improves performance?
val result = numbers
.map { it * 2 }
.filter { it > 10 }
.take(5)
.toList()Think about how lazy evaluation can reduce work done.
Using asSequence() makes the operations lazy, so only necessary elements are processed, improving performance especially with take(5).
Given the code below, what is the output and why?
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.map { it * 2 }.filter { it > 5 }
println(result)Consider the order and eager evaluation of collection operations.
map is applied first, doubling each number, then filter removes those not greater than 5, resulting in [6, 8, 10].