The 'async' keyword marks a method as asynchronous, allowing it to run operations without blocking the main thread and enabling the use of 'await' inside it.
Click to reveal answer
beginner
What is the role of the 'await' keyword in async methods?
'await' pauses the execution of the async method until the awaited task completes, then resumes execution without blocking the thread.
Click to reveal answer
beginner
How does async execution flow affect the main thread?
Async execution allows the main thread to continue running other code while waiting for asynchronous operations to complete, improving responsiveness.
Click to reveal answer
intermediate
What happens when an async method reaches an 'await' statement?
When an async method reaches 'await', it returns control to the caller and schedules the rest of the method to continue after the awaited task finishes.
Click to reveal answer
beginner
Explain the difference between synchronous and asynchronous execution flow.
Synchronous execution runs tasks one after another, blocking the thread until each finishes. Asynchronous execution allows tasks to run without blocking, letting the program do other work while waiting.
Click to reveal answer
What does the 'async' keyword enable in a C# method?
AAllows the method to run without blocking and use 'await'
BMakes the method run faster synchronously
CPrevents the method from returning a value
DDisables exception handling inside the method
✗ Incorrect
'async' allows the method to run asynchronously and use 'await' to pause without blocking.
What happens when 'await' is used on a Task in an async method?
AThe method pauses and the thread is blocked
BThe method runs synchronously without pausing
CThe method pauses but the thread is free to do other work
DThe method throws an exception immediately
✗ Incorrect
'await' pauses the method but frees the thread to continue other work.
Which statement best describes async execution flow?
ATasks run one after another, blocking the thread
BTasks run without blocking, allowing other code to run
CTasks run only on background threads
DTasks run only after the main thread finishes
✗ Incorrect
Async execution allows tasks to run without blocking, so other code can continue.
When an async method hits an 'await', what does it do?
AThrows a runtime error
BBlocks the thread until the task completes
CImmediately completes the method
DReturns control to the caller and resumes later
✗ Incorrect
The method returns control and resumes after the awaited task finishes.
Why is async execution useful in UI applications?
AIt keeps the UI responsive during long operations
BIt makes the UI freeze while waiting
CIt disables user input
DIt slows down the application
✗ Incorrect
Async execution keeps the UI responsive by not blocking the main thread.
Describe how the flow of execution changes when using async and await in C#.
Think about what happens when the method reaches 'await' and how the thread behaves.
You got /5 concepts.
Explain why async execution improves application responsiveness, especially in UI apps.
Consider what happens if the main thread is blocked during a long task.
You got /4 concepts.
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
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.
Step 2: Differentiate from blocking
Blocking the thread would freeze the program, but await allows the thread to be free for other tasks.
Final Answer:
It pauses the method until the awaited task completes without blocking the thread. -> Option C
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
Step 1: Identify correct async method declaration
The correct syntax places the async keyword before the return type: public async void MyMethod().
Step 2: Check other options for syntax errors
Options A, B, and D have incorrect keyword order or placement, which causes syntax errors.
Final Answer:
public async void MyMethod() { } -> Option A
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
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.
Step 2: Understand await behavior
Await pauses MainAsync until GetNumberAsync completes, then prints "Result: 5".
Final Answer:
Before await\nResult: 5 -> Option B
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:
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
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.
Step 2: Understand effect of missing await
Without await, the delay is ignored in terms of method flow, causing unexpected behavior.
Final Answer:
Missing await before Task.Delay, so delay is ignored. -> Option D
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
Step 1: Understand concurrency with async tasks
Running await TaskA(); await TaskB(); runs tasks sequentially, not concurrently.
Step 2: Use Task.WhenAll for parallel execution
await Task.WhenAll(TaskA(), TaskB()); starts both tasks concurrently and awaits their completion together.
Step 3: Evaluate other options
TaskA().Wait(); TaskB().Wait(); blocks threads synchronously, and D starts tasks but does not await completion.
Final Answer:
await Task.WhenAll(TaskA(), TaskB()); -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.