Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Stream vs async stream behavior in C Sharp (C#) - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stream Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of synchronous stream iteration
What is the output of this C# code that uses a synchronous stream?
C Sharp (C#)
using System;
using System.Collections.Generic;

class Program {
    static IEnumerable<int> GetNumbers() {
        for (int i = 1; i <= 3; i++) {
            yield return i;
        }
    }

    static void Main() {
        foreach (var num in GetNumbers()) {
            Console.Write(num + " ");
        }
    }
}
A1 2 3
B1 2
C3 2 1
DCompilation error
Attempts:
2 left
💡 Hint
Remember that yield return produces values one by one in order.
❓ Predict Output
intermediate
2:00remaining
Output of asynchronous stream iteration
What is the output of this C# code that uses an asynchronous stream?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

class Program {
    static async IAsyncEnumerable<int> GetNumbersAsync() {
        for (int i = 1; i <= 3; i++) {
            await Task.Delay(10);
            yield return i;
        }
    }

    static async Task Main() {
        await foreach (var num in GetNumbersAsync()) {
            Console.Write(num + " ");
        }
    }
}
ANo output
B3 2 1
CCompilation error due to missing await
D1 2 3
Attempts:
2 left
💡 Hint
Async streams yield values asynchronously but preserve order.
❓ Predict Output
advanced
2:00remaining
Behavior difference between stream and async stream
What will be the output of this program that mixes synchronous and asynchronous streams?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

class Program {
    static IEnumerable<int> GetSyncNumbers() {
        yield return 1;
        yield return 2;
    }

    static async IAsyncEnumerable<int> GetAsyncNumbers() {
        yield return 3;
        await Task.Delay(10);
        yield return 4;
    }

    static async Task Main() {
        foreach (var n in GetSyncNumbers()) {
            Console.Write(n + " ");
        }
        await foreach (var n in GetAsyncNumbers()) {
            Console.Write(n + " ");
        }
    }
}
A1 2 3 4
B3 4 1 2
C1 2
DCompilation error due to mixing sync and async
Attempts:
2 left
💡 Hint
Synchronous streams run first, then asynchronous streams run with await.
❓ Predict Output
advanced
2:00remaining
Error when using foreach on async stream without await
What error does this code produce when trying to iterate an async stream with a normal foreach?
C Sharp (C#)
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

class Program {
    static async IAsyncEnumerable<int> GetAsyncNumbers() {
        yield return 1;
        await Task.Delay(10);
        yield return 2;
    }

    static void Main() {
        foreach (var n in GetAsyncNumbers()) {
            Console.Write(n + " ");
        }
    }
}
ANo output, program hangs
BRuntime error: InvalidOperationException
CCompilation error: cannot convert IAsyncEnumerable<int> to IEnumerable<int>
DOutput: 1 2
Attempts:
2 left
💡 Hint
Async streams require await foreach, not normal foreach.
🧠 Conceptual
expert
2:00remaining
Why use async streams over synchronous streams?
Which of the following best explains why async streams are preferred over synchronous streams when dealing with data that arrives over time?
AAsync streams always run faster than synchronous streams because they use multiple threads automatically.
BAsync streams allow processing data as it arrives without blocking the thread, improving responsiveness.
CSynchronous streams cannot yield more than 10 items, async streams have no limit.
DAsync streams do not require the use of await, making code simpler.
Attempts:
2 left
💡 Hint
Think about waiting for data and thread blocking.

Practice

(1/5)
1. What is the main difference between a Stream and an async stream in C#?
easy
A. A Stream processes data synchronously, while an async stream processes data asynchronously.
B. A Stream can only read data, but an async stream can only write data.
C. A Stream is used for text data, and an async stream is used for binary data.
D. There is no difference; both work the same way.

Solution

  1. Step 1: Understand Stream behavior

    A Stream reads or writes data step-by-step synchronously, blocking the thread until data is processed.
  2. Step 2: Understand async stream behavior

    An async stream uses IAsyncEnumerable<T> and await foreach to process data asynchronously, allowing other work while waiting for data.
  3. Final Answer:

    A Stream processes data synchronously, while an async stream processes data asynchronously. -> Option A
  4. Quick Check:

    Sync vs async processing = C [OK]
Hint: Sync streams block; async streams use await foreach [OK]
Common Mistakes:
  • Thinking async streams are only for writing data
  • Confusing synchronous with asynchronous processing
  • Believing streams only handle text or binary exclusively
2. Which of the following is the correct syntax to consume an async stream in C#?
easy
A. foreach (var item in asyncStream) { Console.WriteLine(item); }
B. await foreach (var item in asyncStream) { Console.WriteLine(item); }
C. for await (var item in asyncStream) { Console.WriteLine(item); }
D. async foreach (var item in asyncStream) { Console.WriteLine(item); }

Solution

  1. Step 1: Recall async stream consumption syntax

    Async streams require await foreach to asynchronously iterate over IAsyncEnumerable<T>.
  2. Step 2: Check each option

    foreach (var item in asyncStream) { Console.WriteLine(item); } uses regular foreach which is invalid for async streams. await foreach (var item in asyncStream) { Console.WriteLine(item); } correctly uses await foreach. for await (var item in asyncStream) { Console.WriteLine(item); } uses invalid syntax for await. async foreach (var item in asyncStream) { Console.WriteLine(item); } uses invalid keyword order async foreach.
  3. Final Answer:

    await foreach (var item in asyncStream) { Console.WriteLine(item); } -> Option B
  4. Quick Check:

    Async stream needs await foreach = A [OK]
Hint: Use 'await foreach' to read async streams correctly [OK]
Common Mistakes:
  • Using regular foreach with async streams
  • Swapping 'async' and 'await' keywords incorrectly
  • Trying 'for await' which is invalid in C#
3. Consider this code snippet:
async IAsyncEnumerable<int> GetNumbersAsync()
{
    for (int i = 1; i <= 3; i++)
    {
        await Task.Delay(100);
        yield return i;
    }
}

static async Task Main()
{
    await foreach (var num in GetNumbersAsync())
    {
        Console.Write(num + " ");
    }
}
What will be the output when this program runs?
medium
A. Task not awaited error
B. 123
C. 1 2 3
D. No output

Solution

  1. Step 1: Understand async stream generation

    The method yields numbers 1 to 3 with a 100ms delay between each, simulating asynchronous data arrival.
  2. Step 2: Understand async stream consumption

    The await foreach prints each number followed by a space as they arrive asynchronously.
  3. Final Answer:

    1 2 3 -> Option C
  4. Quick Check:

    Async yield with await foreach prints spaced numbers = A [OK]
Hint: Async yield returns spaced values printed by await foreach [OK]
Common Mistakes:
  • Expecting numbers without spaces
  • Thinking output is immediate without delay
  • Confusing async stream with synchronous enumeration
4. What is wrong with this code snippet?
async IAsyncEnumerable<int> GetDataAsync()
{
    for (int i = 0; i < 3; i++)
    {
        yield return i;
        await Task.Delay(100);
    }
}
medium
A. The await must come before yield return in async streams.
B. You cannot use await after yield return in async streams.
C. The method must return IEnumerable<int> instead of IAsyncEnumerable<int>.
D. Nothing is wrong; this code is correct.

Solution

  1. Step 1: Understand async stream yield rules

    In async streams, await must appear before yield return because the compiler needs to await before producing the next value.
  2. Step 2: Analyze the code order

    The code yields a value first, then awaits delay, which is invalid and causes a compile error.
  3. Final Answer:

    The await must come before yield return in async streams. -> Option A
  4. Quick Check:

    Await before yield return in async streams = B [OK]
Hint: Await before yield return in async streams to avoid errors [OK]
Common Mistakes:
  • Placing yield return before await in async streams
  • Confusing return types for async streams
  • Assuming no await is needed in async streams
5. You want to process a large file line-by-line without blocking your UI thread. Which approach best fits this scenario in C#?
hard
A. Use StreamReader.ReadLine() inside a synchronous foreach loop.
B. Use a regular Stream and block until all data is read.
C. Read the entire file into memory first, then process lines synchronously.
D. Use IAsyncEnumerable<string> with await foreach reading lines asynchronously.

Solution

  1. Step 1: Identify UI thread blocking issue

    Reading large files synchronously blocks the UI thread, causing freezes.
  2. Step 2: Choose async streaming approach

    Using IAsyncEnumerable<string> with await foreach reads lines asynchronously, allowing UI responsiveness.
  3. Final Answer:

    Use IAsyncEnumerable<string> with await foreach reading lines asynchronously. -> Option D
  4. Quick Check:

    Async streams keep UI responsive during file read = D [OK]
Hint: Async streams prevent UI blocking when reading large files [OK]
Common Mistakes:
  • Reading files synchronously causing UI freeze
  • Loading entire file into memory unnecessarily
  • Using blocking Stream methods on UI thread
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