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

Async and await keywords in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
ACompilation error
BTask
Cnull
DHello
Attempts:
2 left
💡 Hint
Remember that await waits for the Task to complete and returns the result.
Predict Output
intermediate
2: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);
    }
}
A10
B0
CDeadlock or runtime error
DCompilation error
Attempts:
2 left
💡 Hint
Accessing Task.Result blocks until the task completes.
Predict Output
advanced
2: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();
    }
}
APrints "Done" then crashes with unhandled exception
BPrints "Done" and exits normally
CDeadlock occurs
DCompilation error due to async void
Attempts:
2 left
💡 Hint
Exceptions in async void methods are not caught by callers.
🧠 Conceptual
advanced
2:00remaining
Understanding async method return types
Which of the following is a valid return type for an async method in C#?
Aasync Task<int>
Basync void
Casync int
Dasync Task
Attempts:
2 left
💡 Hint
Async methods can return Task, Task, or void (rarely).
Predict Output
expert
3: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);
        }
    }
}
A
Data2
Data3
Data1
B
Data1
Data2
Data3
C
Data3
Data1
Data2
DCompilation error
Attempts:
2 left
💡 Hint
Task.WhenAll preserves the order of the tasks in the results array.