Functional Streams and Lazy Evaluation Fundamentals Quiz Quiz

Explore the essentials of functional streams and lazy evaluation with this easy-level quiz. Assess your understanding of key concepts like stream processing, immutability, infinite sequences, and deferred computation in functional programming.

  1. Concept of Streams

    Which characteristic best describes a functional stream in programming?

    1. A graphical user interface element
    2. A variable that holds a single value
    3. A fixed-size list stored in memory
    4. A sequence of elements computed on demand

    Explanation: Functional streams provide elements as needed, which makes them suitable for processing potentially large or infinite data sets. A fixed-size list refers to traditional arrays or lists, which are fully realized in memory. A variable holding a single value is not a stream, and graphical elements are unrelated to the concept of streams in computation.

  2. Understanding Lazy Evaluation

    What is the main benefit of lazy evaluation in functional programming?

    1. Programs run in random order
    2. Data is always saved to disk
    3. Computation is performed only when needed
    4. Variables must be declared before use

    Explanation: Lazy evaluation delays calculation until a value is required, conserving memory and processing time. Saving data to disk is unrelated to evaluation strategy, variable declaration is a separate language rule, and program execution order remains determined even with lazy evaluation.

  3. Streams vs Lists

    How does a stream differ from a regular list regarding memory usage?

    1. A stream is pre-loaded entirely into memory
    2. A stream always uses more memory by storing all elements twice
    3. A list uses lazy evaluation for all operations
    4. A stream requires less memory since elements are generated only as requested

    Explanation: Streams are typically more memory-efficient since they compute and hold only needed elements at any given time. Storing elements twice is inaccurate; lists are usually eagerly evaluated rather than lazy, and pre-loading the entire stream contradicts its very purpose.

  4. Infinite Sequences

    Which case is an advantage of using lazy streams to represent infinite sequences like the Fibonacci numbers?

    1. Only the required number of elements are computed
    2. Computation happens instantly for all values
    3. All infinite elements are stored beforehand
    4. Only finite sequences are possible

    Explanation: With lazily evaluated streams, only the needed entries—such as the first ten Fibonacci numbers—are ever created, allowing for infinite sequence definitions. Storing all infinite elements is impossible, computation is on demand (not instant for all), and lazy streams specifically enable infinite, not only finite, sequences.

  5. Immutability in Streams

    Why are streams often implemented as immutable collections in functional programming?

    1. Immutability prevents accidental side effects during processing
    2. Immutability makes streams slower
    3. Immutability allows values to be changed anytime
    4. Streams cannot process any elements if immutable

    Explanation: Immutability ensures that processing streams does not alter the data, maintaining system predictability. Changing values anytime contradicts immutability, making streams slower is a misconception, and immutability does not prevent streams from processing elements.

  6. Stream Filters

    What does applying a filter operation to a lazy stream achieve?

    1. It returns a new stream that only computes filtered elements on access
    2. It eagerly processes all elements before returning them
    3. It sorts the stream in ascending order
    4. It deletes elements from the original stream in memory

    Explanation: A filter applied to a lazy stream creates another stream which yields elements satisfying the condition, but calculates them only when accessed. Filtering does not mutate the original data, does not automatically sort, and does not perform eager evaluation.

  7. Chaining Operations

    Which of the following statements is true about chaining multiple operations on a lazy stream?

    1. Operations are not executed until the result is requested
    2. Only one operation can be chained at a time
    3. All operations must modify the original stream
    4. Every operation is executed immediately upon declaration

    Explanation: With lazy evaluation, chained operations are just recorded; actual processing happens only when a result is eventually needed. Immediate execution is a property of eager evaluation, and streams are typically immutable, so original data is not changed. Multiple operations can be chained freely.

  8. Mapping Over Streams

    What happens when a map function is applied to a lazy stream of numbers, such as incrementing each number by one?

    1. The original stream is destroyed
    2. A new stream is created that computes incremented values as they are accessed
    3. All values are incremented immediately
    4. The stream’s data type changes to characters

    Explanation: The map operation returns a new lazy stream so that values are incremented on demand. Destroying the original stream does not occur, immediate computation is contrary to lazy evaluation, and the data type remains unchanged unless explicitly transformed.

  9. Stream Termination and Evaluation

    Why does evaluating a lazy stream usually require a terminal operation like collecting to a list or summing values?

    1. Because streams execute all code at program start
    2. Because lazy streams only compute values when a final result is needed
    3. Because terminal operations are required to write code
    4. Because collecting is needed to change the stream’s type to integer

    Explanation: Terminal operations such as conversion to a list or calculation cause the deferred computations of the lazy stream to be performed. Execution at program start applies to eager models, not lazy ones. Changing the type is not the sole purpose of terminal operations, and code writing does not require them by definition.

  10. Common Use of Lazy Streams

    Which programming scenario best benefits from lazy streams?

    1. Sorting a very small array of numbers
    2. Processing a very large or infinite data set where only part of the data is needed
    3. Performing simple arithmetic on a single value
    4. Storing a constant configuration value

    Explanation: Lazy streams shine when handling large or infinite collections, avoiding unnecessary computation and memory use by generating only the required elements. Small arrays or individual values do not require lazy streams, and constant configuration data is best handled by simple variables.