Consider the following Kotlin code using a Sequence and a Java Stream. What will be the output?
val list = listOf(1, 2, 3, 4) val seq = list.asSequence() .map { println("Sequence map: $it") it * 2 } .filter { println("Sequence filter: $it") it > 4 } println("Sequence result: ${seq.toList()}")
Remember that Kotlin Sequence operations are lazy and process elements one by one.
Kotlin Sequence processes elements lazily, so map and filter are called per element in order. The output shows the interleaved calls for each element before moving to the next.
Which statement best describes the difference between Java Streams and Kotlin Sequences?
Think about how each handles intermediate operations and when elements are processed.
Kotlin Sequences are lazy and process elements one by one through the chain of operations. Java Streams are also lazy but may internally optimize processing in batches depending on the terminal operation.
Look at this Kotlin code using a Sequence. Why does it not print anything?
val seq = sequenceOf(1, 2, 3) .map { println("Mapping $it") it * 2 } // No terminal operation here
Think about when lazy operations run in Kotlin Sequences.
Kotlin Sequences are lazy. Intermediate operations like map do not run until a terminal operation (like toList()) is called.
Which option contains a syntax error in this Java Stream code snippet?
List<Integer> numbers = List.of(1, 2, 3, 4); var result = numbers.stream() .map(n -> n * 2) .filter(n -> n > 4) .collect(Collectors.toList());
Look carefully at the parentheses and method calls.
Option A is missing the closing parenthesis after toList, causing a syntax error.
You need to process a very large list of data with multiple transformations and filters. Which approach is best to minimize memory usage and why?
Think about lazy processing and memory consumption.
Kotlin Sequences process elements lazily and one at a time, which helps keep memory usage low when handling large data sets. Java Streams can also be lazy but may have different internal optimizations.