0
0
C Sharp (C#)programming~20 mins

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

Choose your learning style9 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.