Async and Await Essentials in C# Quiz

Challenge your understanding of asynchronous programming in C# using async and await, covering core concepts, common patterns, and potential pitfalls. Improve your grasp of modern concurrency techniques and effective management of asynchronous tasks in C#.

  1. Understanding the Purpose of async

    Which keyword must be added to a C# method declaration to enable the use of the await operator inside the method?

    1. async
    2. awaiter
    3. threaded
    4. synchronous

    Explanation: The async keyword allows a method to use the await operator, enabling asynchronous operations within that method. Adding threaded or synchronous does not provide support for awaiting asynchronous operations. Awaiter is not a keyword in C#; it's a class related to the await pattern but not used in method declarations.

  2. Return Types of Async Methods

    What is the most common return type for a C# async method that does not return a value?

    1. Task
    2. void
    3. Promise
    4. int

    Explanation: Task is the correct and recommended return type for async methods that do not return a value, as it conveys asynchronous work and allows monitoring of completion. Using void is reserved for event handlers in asynchronous code. Promise is not a C# type, and int would only be appropriate for a synchronous method returning an integer.

  3. Behavior of await

    What does the await keyword do when used before a method returning Task in C#?

    1. It converts the method into synchronous code.
    2. It pauses execution of the method until the Task completes.
    3. It creates a new thread.
    4. It cancels the Task if it takes too long.

    Explanation: Await pauses the execution of the method until the Task completes, resuming at the point where it was left off. It does not create a new thread; scheduling is managed by the runtime. Await does not convert code to be synchronous, nor does it cancel tasks automatically if they take too long.

  4. Async Methods and Exceptions

    How are exceptions thrown inside an awaited async method typically handled in C# if the Task is awaited?

    1. They cause automatic application termination.
    2. They are ignored and the application continues.
    3. They are re-thrown at the point of the await statement.
    4. They are logged silently without programmer intervention.

    Explanation: When an exception is thrown in an awaited async method, it is captured by the Task and re-thrown at the await statement. This enables standard try-catch exception handling. Ignoring exceptions or silent logging is incorrect, and async exceptions do not automatically terminate the application.

  5. Async Event Handler Signature

    Which return type is recommended for asynchronous event handlers in C#?

    1. Func
    2. void
    3. async
    4. Task

    Explanation: Asynchronous event handlers in C# should have a void return type to match the delegate signature for events. Using Task is not permitted for event handlers, while async is a modifier, not a return type. Func is a delegate type, not a method return type.

  6. Calling Async Methods

    What happens if you call an async method without using await or capturing its returned Task?

    1. The method runs asynchronously, but exceptions may be unobserved.
    2. The compiler throws an error.
    3. The method executes synchronously.
    4. The method does not run at all.

    Explanation: Calling an async method without await causes it to start asynchronously, but any exceptions may go unobserved and can lead to application crashes or unpredictable behavior. There is no compiler error, and the method does not execute synchronously, nor is it skipped entirely.

  7. Task.FromResult Usage

    Which method produces a completed Task with a specified result and is commonly used in async methods for quick-return data?

    1. Task.FromResult
    2. Task.Run
    3. Task.Await
    4. Task.Completed

    Explanation: Task.FromResult quickly produces a completed Task with a given result and is often used for returning cached or computed data in async APIs. Task.Run schedules work asynchronously on a thread pool but does not return a completed task instantly. Task.Completed and Task.Await are not valid C# methods.

  8. Avoiding Deadlocks with ConfigureAwait

    Which of the following statements about ConfigureAwait(false) is correct in C# async programming?

    1. It cancels the Task if it takes too long.
    2. It forces the method to run on the main thread.
    3. It avoids capturing the original synchronization context when resuming after await.
    4. It retries the Task if it fails.

    Explanation: ConfigureAwait(false) prevents resuming on the original synchronization context, which helps avoid deadlocks in certain scenarios. It does not retry or cancel tasks, nor does it force main thread execution. Using ConfigureAwait(false) is particularly important in library code.

  9. Writing Async Lambda Expressions

    How do you correctly declare an asynchronous lambda expression in C#?

    1. By using await as the lambda parameter
    2. By using the async keyword after the lambda
    3. By writing await in the lambda body only
    4. By prefixing the lambda with async, like async () =u003E { ... }

    Explanation: Declaring an asynchronous lambda requires prefixing it with async. Placing async after the lambda is invalid syntax. Simply using await in the body is not enough; the lambda must be declared async. Await cannot be used as a parameter name or type.

  10. Async Methods and Main Entry Point

    Starting with which version of C#, can you declare the Main method as async Task Main()?

    1. C# 5.0
    2. C# 4.0
    3. C# 7.1
    4. C# 3.0

    Explanation: Support for async Task Main entry points was introduced in C# 7.1, allowing asynchronous code execution in the main method. Earlier versions, such as 5.0, 4.0, or 3.0, do not support this syntax, so using async in the Main method is not possible there.