Challenge - 5 Problems
Stream Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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 + " "); } } }
Attempts:
2 left
💡 Hint
Remember that yield return produces values one by one in order.
✗ Incorrect
The synchronous iterator yields numbers 1, 2, and 3 in order, so the output is '1 2 3 '.
❓ Predict Output
intermediate2: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 + " "); } } }
Attempts:
2 left
💡 Hint
Async streams yield values asynchronously but preserve order.
✗ Incorrect
The async iterator yields numbers 1, 2, and 3 in order with delays, so output is '1 2 3 '.
❓ Predict Output
advanced2: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 + " "); } } }
Attempts:
2 left
💡 Hint
Synchronous streams run first, then asynchronous streams run with await.
✗ Incorrect
The synchronous stream yields 1 and 2 immediately, then the async stream yields 3 and 4 asynchronously, so output is '1 2 3 4 '.
❓ Predict Output
advanced2: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 + " "); } } }
Attempts:
2 left
💡 Hint
Async streams require await foreach, not normal foreach.
✗ Incorrect
The compiler cannot use normal foreach on IAsyncEnumerable, so it raises a compilation error.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about waiting for data and thread blocking.
✗ Incorrect
Async streams let you wait for data asynchronously, so the program stays responsive and doesn't block while waiting.