Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

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

Practice

(1/5)
1. What does the await keyword do in an async method in C#?
easy
A. It immediately blocks the thread until the awaited task completes.
B. It converts a synchronous method into an asynchronous one automatically.
C. It pauses the method until the awaited task completes without blocking the thread.
D. It cancels the running async method.

Solution

  1. Step 1: Understand the role of await

    The await keyword pauses the async method execution until the awaited task finishes, but it does not block the thread, allowing other work to continue.
  2. Step 2: Differentiate from blocking

    Blocking the thread would freeze the program, but await allows the thread to be free for other tasks.
  3. Final Answer:

    It pauses the method until the awaited task completes without blocking the thread. -> Option C
  4. Quick Check:

    await pauses without blocking [OK]
Hint: Remember: await pauses method but frees thread [OK]
Common Mistakes:
  • Thinking await blocks the thread
  • Confusing await with async keyword
  • Assuming await cancels tasks
2. Which of the following is the correct syntax to declare an asynchronous method in C#?
easy
A. public async void MyMethod() { }
B. public void async MyMethod() { }
C. async public void MyMethod() { }
D. public void MyMethod async() { }

Solution

  1. Step 1: Identify correct async method declaration

    The correct syntax places the async keyword before the return type: public async void MyMethod().
  2. Step 2: Check other options for syntax errors

    Options A, B, and D have incorrect keyword order or placement, which causes syntax errors.
  3. Final Answer:

    public async void MyMethod() { } -> Option A
  4. Quick Check:

    async keyword before return type [OK]
Hint: Put async before return type in method declaration [OK]
Common Mistakes:
  • Placing async after return type
  • Using async as a method modifier incorrectly
  • Adding parentheses after async
3. What will be the output of the following C# code?
async Task<int> GetNumberAsync() {
    await Task.Delay(100);
    return 5;
}

async Task MainAsync() {
    var task = GetNumberAsync();
    Console.WriteLine("Before await");
    int result = await task;
    Console.WriteLine($"Result: {result}");
}

MainAsync().Wait();
medium
A. No output due to deadlock
B. Before await\nResult: 5
C. Before await
D. Result: 5\nBefore await

Solution

  1. Step 1: Analyze the order of execution

    The method prints "Before await" immediately after starting the async task, then awaits the task completion before printing the result.
  2. Step 2: Understand await behavior

    Await pauses MainAsync until GetNumberAsync completes, then prints "Result: 5".
  3. Final Answer:

    Before await\nResult: 5 -> Option B
  4. Quick Check:

    Print before await, then after task completes [OK]
Hint: Output before await prints first, then awaited result [OK]
Common Mistakes:
  • Assuming result prints before 'Before await'
  • Thinking await blocks main thread
  • Expecting no output due to deadlock
4. Identify the problem in this async code snippet:
async Task<int> CalculateAsync() {
    Task.Delay(1000);
    return 10;
}
medium
A. Method should not be async if it returns a value.
B. Return type should be void, not Task<int>.
C. Task.Delay cannot be used inside async methods.
D. Missing await before Task.Delay, so delay is ignored.

Solution

  1. Step 1: Check usage of Task.Delay

    The method calls Task.Delay(1000) but does not await it, so the delay runs but the method immediately returns 10.
  2. Step 2: Understand effect of missing await

    Without await, the delay is ignored in terms of method flow, causing unexpected behavior.
  3. Final Answer:

    Missing await before Task.Delay, so delay is ignored. -> Option D
  4. Quick Check:

    Always await async calls to run properly [OK]
Hint: Always await async calls like Task.Delay [OK]
Common Mistakes:
  • Forgetting await before async calls
  • Changing return type unnecessarily
  • Thinking Task.Delay is invalid in async
5. You want to run two independent async tasks TaskA() and TaskB() concurrently and wait for both to finish before continuing. Which code snippet correctly achieves this?
hard
A. await Task.WhenAll(TaskA(), TaskB());
B. await TaskA(); await TaskB();
C. TaskA().Wait(); TaskB().Wait();
D. Task.Run(() => TaskA()); Task.Run(() => TaskB());

Solution

  1. Step 1: Understand concurrency with async tasks

    Running await TaskA(); await TaskB(); runs tasks sequentially, not concurrently.
  2. Step 2: Use Task.WhenAll for parallel execution

    await Task.WhenAll(TaskA(), TaskB()); starts both tasks concurrently and awaits their completion together.
  3. Step 3: Evaluate other options

    TaskA().Wait(); TaskB().Wait(); blocks threads synchronously, and D starts tasks but does not await completion.
  4. Final Answer:

    await Task.WhenAll(TaskA(), TaskB()); -> Option A
  5. Quick Check:

    Use Task.WhenAll to await multiple tasks concurrently [OK]
Hint: Use Task.WhenAll to await multiple tasks together [OK]
Common Mistakes:
  • Awaiting tasks one after another (sequential)
  • Blocking with Wait() causing deadlocks
  • Starting tasks without awaiting completion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard