Bird
Raised Fist0
Kotlinprogramming~20 mins

Terminal operations trigger execution in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sequence Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this Kotlin sequence operation?
Consider the following Kotlin code using sequences. What will be printed when the code runs?
Kotlin
val numbers = sequenceOf(1, 2, 3, 4, 5)
val result = numbers
    .map {
        println("Mapping $it")
        it * 2
    }
    .filter {
        println("Filtering $it")
        it > 5
    }
println("Before terminal operation")
val output = result.toList()
println("Output: $output")
A
Before terminal operation
Mapping 1
Mapping 2
Filtering 2
Mapping 3
Filtering 4
Mapping 4
Filtering 6
Mapping 5
Filtering 8
Filtering 10
Output: [6, 8, 10]
B
Mapping 1
Mapping 2
Mapping 3
Mapping 4
Mapping 5
Filtering 2
Filtering 4
Filtering 6
Filtering 8
Filtering 10
Before terminal operation
Output: [6, 8, 10]
C
Before terminal operation
Mapping 1
Filtering 2
Mapping 2
Filtering 4
Mapping 3
Filtering 6
Mapping 4
Filtering 8
Mapping 5
Filtering 10
Output: [6, 8, 10]
D
Before terminal operation
Output: []
Attempts:
2 left
💡 Hint
Remember that sequences in Kotlin are lazy and only execute when a terminal operation is called.
🧠 Conceptual
intermediate
1:30remaining
Why do terminal operations trigger execution in Kotlin sequences?
In Kotlin, why do terminal operations like toList() or count() cause the sequence to execute, while intermediate operations like map() or filter() do not?
ABecause intermediate operations only build a processing pipeline without processing elements, and terminal operations start the actual processing.
BBecause intermediate operations are executed immediately, but terminal operations delay execution until needed.
CBecause terminal operations cache the results, while intermediate operations discard them.
DBecause Kotlin sequences do not support intermediate operations without terminal operations.
Attempts:
2 left
💡 Hint
Think about lazy evaluation and how sequences process elements.
📝 Syntax
advanced
1:30remaining
Which Kotlin code snippet correctly triggers execution of a sequence?
Given a Kotlin sequence, which of the following code snippets correctly triggers its execution?
A
val seq = sequenceOf(1, 2, 3).map { it * 2 }
seq.forEach { it }
B
val seq = sequenceOf(1, 2, 3).map { it * 2 }
// No terminal operation here
C
val seq = sequenceOf(1, 2, 3).map { it * 2 }
println(seq)
Dval seq = sequenceOf(1, 2, 3).map { it * 2 }.toList()
Attempts:
2 left
💡 Hint
Which operation forces the sequence to process its elements?
🔧 Debug
advanced
2:00remaining
Why does this Kotlin sequence code print nothing?
Analyze the following Kotlin code. Why does it print nothing?
Kotlin
val seq = sequenceOf(1, 2, 3, 4)
seq.map {
    println("Processing $it")
    it * 10
}
ABecause the println statement is inside map and map does not execute at all.
BBecause map is an intermediate operation and no terminal operation is called to trigger execution.
CBecause sequenceOf creates an empty sequence by default.
DBecause the map operation returns a list, but it is not printed.
Attempts:
2 left
💡 Hint
Think about when sequences actually run their operations.
❓ optimization
expert
2:30remaining
How to optimize Kotlin sequence processing to avoid unnecessary computations?
Given a Kotlin sequence with multiple intermediate operations, which approach optimizes execution by avoiding processing elements unnecessarily?
AUse terminal operations like first() or find() to stop processing early when a condition is met.
BCall toList() after every intermediate operation to force execution step-by-step.
CAvoid terminal operations to keep the sequence lazy and never execute it.
DUse map() to transform all elements before filtering them with filter().
Attempts:
2 left
💡 Hint
Think about how terminal operations can stop processing early.

Practice

(1/5)
1. What does a terminal operation do in Kotlin sequences or collections?
toList(), count(), and forEach() are examples of terminal operations. What is their main role?
easy
A. They start the actual processing and produce results or side effects.
B. They define the data source without processing it.
C. They create new sequences without executing any code.
D. They only prepare the data but do not execute any operation.

Solution

  1. Step 1: Understand what terminal operations are

    Terminal operations are the final step in a sequence or collection processing chain that triggers execution.
  2. Step 2: Identify the role of examples like toList(), count(), and forEach()

    These operations produce a result or cause side effects, thus starting the actual processing.
  3. Final Answer:

    They start the actual processing and produce results or side effects. -> Option A
  4. Quick Check:

    Terminal operations trigger execution = B [OK]
Hint: Terminal operations always trigger execution and produce results [OK]
Common Mistakes:
  • Thinking terminal operations only prepare data without running it
  • Confusing terminal operations with intermediate operations
  • Believing terminal operations do not produce results
2. Which of the following is a correct Kotlin terminal operation syntax on a list named numbers?
easy
A. numbers.toList()
B. numbers.map()
C. numbers.filter()
D. numbers.sortedBy

Solution

  1. Step 1: Identify terminal operations syntax

    Terminal operations like toList() are called with parentheses to execute and produce results.
  2. Step 2: Check each option

    map() and filter() are intermediate operations requiring arguments; sortedBy is incomplete without parentheses.
  3. Final Answer:

    numbers.toList() -> Option A
  4. Quick Check:

    Correct terminal operation syntax = A [OK]
Hint: Terminal operations have parentheses and produce results [OK]
Common Mistakes:
  • Using intermediate operations as terminal without arguments
  • Omitting parentheses on function calls
  • Confusing intermediate and terminal operations
3. Consider this Kotlin code:
val numbers = sequenceOf(1, 2, 3, 4)
val result = numbers.filter { it % 2 == 0 }.count()

What is the value of result after execution?
medium
A. 0
B. 4
C. 2
D. Sequence<Int>

Solution

  1. Step 1: Apply the filter condition

    The filter keeps only even numbers: 2 and 4.
  2. Step 2: Count the filtered elements

    There are 2 even numbers, so count() returns 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    Count of even numbers = 2 [OK]
Hint: Filter then count gives number of matching elements [OK]
Common Mistakes:
  • Counting before filtering
  • Assuming count returns the sequence itself
  • Confusing sequence with list output
4. What is wrong with this Kotlin code snippet?
val numbers = listOf(1, 2, 3)
numbers.filter { it > 1 }

Why does it not print or return any filtered list?
medium
A. The variable numbers is immutable.
B. Missing a terminal operation to trigger execution.
C. List cannot be filtered in Kotlin.
D. The filter condition is invalid syntax.

Solution

  1. Step 1: Understand filter is intermediate

    The filter function returns a new list but does not execute anything by itself.
  2. Step 2: Identify missing terminal operation

    Without a terminal operation like toList() or forEach(), the filtered list is not processed or printed.
  3. Final Answer:

    Missing a terminal operation to trigger execution. -> Option B
  4. Quick Check:

    Intermediate operations need terminal to run = D [OK]
Hint: Always end with terminal operation to see results [OK]
Common Mistakes:
  • Thinking filter runs immediately
  • Assuming immutability blocks filtering
  • Believing filter syntax is wrong
5. Given this Kotlin code:
val data = sequenceOf(0, 1, 2, 3, 4, 5)
val result = data
  .filter { it > 0 }
  .map { if (it % 2 == 0) it * 10 else it }
  .toList()

What is the content of result after execution?
hard
A. [10, 20, 30, 40, 50]
B. [0, 1, 2, 3, 4, 5]
C. [1, 2, 3, 4, 5]
D. [1, 20, 3, 40, 5]

Solution

  1. Step 1: Filter out values greater than 0

    Sequence after filter: 1, 2, 3, 4, 5 (0 is removed).
  2. Step 2: Map even numbers to value * 10, odd numbers unchanged

    1 (odd) -> 1, 2 (even) -> 20, 3 (odd) -> 3, 4 (even) -> 40, 5 (odd) -> 5.
  3. Step 3: Convert sequence to list with toList()

    Final list: [1, 20, 3, 40, 5].
  4. Final Answer:

    [1, 20, 3, 40, 5] -> Option D
  5. Quick Check:

    Filter then map then toList = C [OK]
Hint: Filter first, then map, then terminal operation to get result [OK]
Common Mistakes:
  • Forgetting filter removes 0
  • Mapping all numbers instead of only even
  • Missing toList() to trigger execution
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard