0
0
Kotlinprogramming~20 mins

Java streams vs Kotlin sequences - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Streams and Sequences Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Java Stream vs Kotlin Sequence Lazy Evaluation

Consider the following Kotlin code using a Sequence and a Java Stream. What will be the output?

Kotlin
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()}")
A
Sequence map: 1
Sequence map: 2
Sequence map: 3
Sequence map: 4
Sequence filter: 2
Sequence filter: 4
Sequence filter: 6
Sequence filter: 8
Sequence result: [6, 8]
B
Sequence map: 1
Sequence filter: 2
Sequence map: 2
Sequence filter: 4
Sequence map: 3
Sequence filter: 6
Sequence result: [6]
C
Sequence map: 1
Sequence filter: 2
Sequence map: 2
Sequence filter: 4
Sequence map: 3
Sequence filter: 6
Sequence map: 4
Sequence filter: 8
Sequence result: [6, 8]
D
Sequence map: 1
Sequence filter: 2
Sequence map: 2
Sequence filter: 4
Sequence map: 3
Sequence filter: 6
Sequence map: 4
Sequence filter: 8
Sequence result: [2, 4, 6, 8]
Attempts:
2 left
💡 Hint

Remember that Kotlin Sequence operations are lazy and process elements one by one.

🧠 Conceptual
intermediate
1:30remaining
Difference in Evaluation Strategy Between Java Streams and Kotlin Sequences

Which statement best describes the difference between Java Streams and Kotlin Sequences?

AKotlin Sequences process elements lazily one by one, while Java Streams may process elements in batches or eagerly depending on the terminal operation.
BBoth Java Streams and Kotlin Sequences process elements eagerly by default.
CJava Streams are always lazy, while Kotlin Sequences are always eager.
DJava Streams process elements one by one lazily, but Kotlin Sequences process all elements eagerly before any operation.
Attempts:
2 left
💡 Hint

Think about how each handles intermediate operations and when elements are processed.

🔧 Debug
advanced
1:30remaining
Why Does This Kotlin Sequence Code Not Print Anything?

Look at this Kotlin code using a Sequence. Why does it not print anything?

Kotlin
val seq = sequenceOf(1, 2, 3)
    .map {
        println("Mapping $it")
        it * 2
    }

// No terminal operation here
ABecause <code>map</code> is executed immediately but the output is buffered and not printed.
BBecause <code>println</code> inside <code>map</code> is ignored in sequences.
CBecause <code>sequenceOf</code> creates an empty sequence by default.
DBecause <code>map</code> is not called until a terminal operation is invoked on the sequence.
Attempts:
2 left
💡 Hint

Think about when lazy operations run in Kotlin Sequences.

📝 Syntax
advanced
1:30remaining
Identify the Syntax Error in Java Stream Code

Which option contains a syntax error in this Java Stream code snippet?

Kotlin
List<Integer> numbers = List.of(1, 2, 3, 4);

var result = numbers.stream()
    .map(n -> n * 2)
    .filter(n -> n > 4)
    .collect(Collectors.toList());
A
var result = numbers.stream()
    .map(n -&gt; n * 2)
    .filter(n -&gt; n &gt; 4)
    .collect(Collectors.toList
B
var result = numbers.stream()
    .map(n -&gt; n * 2)
    .filter(n -&gt; n &gt; 4)
    .collect(Collectors.toList());
C
;))(tsiLot.srotcelloC(tcelloc.    
)4 &gt; n &gt;- n(retlif.    
)2 * n &gt;- n(pam.    
)(maerts.srebmun = tluser rav
D
ar result = numbers.stream()
    .map(n -&gt; n * 2)
    .filter(n -&gt; n &gt; 4)
    .collect(Collectors.toList());
Attempts:
2 left
💡 Hint

Look carefully at the parentheses and method calls.

🚀 Application
expert
2:30remaining
Choosing Between Java Streams and Kotlin Sequences for Large Data Processing

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?

AUse Java Streams because they always process elements eagerly, which is faster for large data.
BUse Kotlin Sequences because they process elements lazily one by one, reducing memory overhead.
CUse Kotlin Sequences because they convert the entire list to a new list before processing, improving speed.
DUse Java Streams because they store all intermediate results in memory, which reduces CPU usage.
Attempts:
2 left
💡 Hint

Think about lazy processing and memory consumption.