Challenge - 5 Problems
Async Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of async method with Task.Delay
What is the output of this C# program when run?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task<string> GetMessageAsync() { await Task.Delay(100); return "Hello"; } static async Task Main() { var message = await GetMessageAsync(); Console.WriteLine(message); } }
Attempts:
2 left
💡 Hint
Remember that await waits for the Task to complete and returns the result.
✗ Incorrect
The method GetMessageAsync returns a Task. Await waits for the delay and then returns "Hello". The Main method prints this string.
❓ Predict Output
intermediate2:00remaining
Value of variable after async method call
What is the value of variable 'result' after running this code?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task<int> CalculateAsync() { await Task.Delay(50); return 10; } static async Task Main() { var task = CalculateAsync(); int result = task.Result; Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
Accessing Task.Result blocks until the task completes.
✗ Incorrect
Calling task.Result blocks the Main thread until CalculateAsync completes and returns 10. Then result is 10.
❓ Predict Output
advanced2:00remaining
Output of async void method with exception
What happens when this program runs?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async void ThrowExceptionAsync() { await Task.Delay(10); throw new InvalidOperationException("Error"); } static void Main() { ThrowExceptionAsync(); Console.WriteLine("Done"); Task.Delay(50).Wait(); } }
Attempts:
2 left
💡 Hint
Exceptions in async void methods are not caught by callers.
✗ Incorrect
The async void method throws an exception after delay. The Main method prints "Done" and waits. The exception is unhandled and crashes the program after printing.
🧠 Conceptual
advanced2:00remaining
Understanding async method return types
Which of the following is a valid return type for an async method in C#?
Attempts:
2 left
💡 Hint
Async methods can return Task, Task, or void (rarely).
✗ Incorrect
Valid async method return types are Task, Task, or void. The keyword async is used before the method, not in the return type.
❓ Predict Output
expert3:00remaining
Output of concurrent async calls with await
What is the output of this program?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task<string> GetDataAsync(int id) { await Task.Delay(50 * id); return $"Data{id}"; } static async Task Main() { var task1 = GetDataAsync(3); var task2 = GetDataAsync(1); var task3 = GetDataAsync(2); var results = await Task.WhenAll(task1, task2, task3); foreach (var r in results) { Console.WriteLine(r); } } }
Attempts:
2 left
💡 Hint
Task.WhenAll preserves the order of the tasks in the results array.
✗ Incorrect
Task.WhenAll returns results in the order tasks were passed, regardless of completion order. task1 (GetDataAsync(3)) returns "Data3", task2 ("Data1"), task3 ("Data2"). So output is Data3
Data1
Data2.