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

How async execution flows in C Sharp (C#) - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Execution Master
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 Main() {
        Console.WriteLine("Start");
        await Task.Delay(100);
        Console.WriteLine("End");
    }
}
AStart
BEnd\nStart
CEnd
DStart\nEnd
Attempts:
2 left
💡 Hint
Remember that await pauses the method until the delay finishes.
Predict Output
intermediate
2:00remaining
Order of execution with async void
What will be printed when this program runs?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static async void PrintAsync() {
        Console.WriteLine("Before delay");
        await Task.Delay(50);
        Console.WriteLine("After delay");
    }

    static void Main() {
        PrintAsync();
        Console.WriteLine("Main finished");
        Task.Delay(100).Wait();
    }
}
AMain finished\nBefore delay\nAfter delay
BBefore delay\nMain finished\nAfter delay
CBefore delay\nAfter delay\nMain finished
DMain finished\nAfter delay\nBefore delay
Attempts:
2 left
💡 Hint
async void methods start running synchronously until the first await.
🔧 Debug
advanced
3:00remaining
Identify the cause of deadlock in async code
This code causes a deadlock when run. What is the reason?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static void Main() {
        var result = GetData().Result;
        Console.WriteLine(result);
    }

    static async Task<string> GetData() {
        await Task.Delay(100);
        return "Done";
    }
}
ABlocking on .Result causes deadlock because the async method tries to resume on the captured context which is blocked.
BTask.Delay(100) throws an exception causing deadlock.
CThe async method GetData is missing await keyword causing deadlock.
DConsole.WriteLine blocks the thread causing deadlock.
Attempts:
2 left
💡 Hint
Think about how .Result blocks the main thread and how async continuations work.
Predict Output
advanced
2:00remaining
Output of async method with ConfigureAwait(false)
What will this program print?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static async Task Main() {
        Console.WriteLine("Start");
        await PrintMessageAsync();
        Console.WriteLine("End");
    }

    static async Task PrintMessageAsync() {
        await Task.Delay(50).ConfigureAwait(false);
        Console.WriteLine("Inside async method");
    }
}
AStart\nInside async method\nEnd
BStart\nEnd\nInside async method
CInside async method\nStart\nEnd
DStart\nEnd
Attempts:
2 left
💡 Hint
ConfigureAwait(false) affects context capture but does not change the order of awaited code.
🧠 Conceptual
expert
3:00remaining
Understanding async execution and thread usage
Which statement best describes how async/await affects thread usage in C#?
AAsync/await creates a new thread for each awaited operation to run in parallel.
BAsync/await blocks the current thread until the awaited task completes, then continues synchronously.
CAsync/await frees the current thread during awaits, allowing it to do other work, and resumes on a thread from the thread pool or the captured context.
DAsync/await guarantees that code after await runs on the same thread that started the async method.
Attempts:
2 left
💡 Hint
Think about what happens to the thread when an await is hit.