Sample questions
Csharp Concurrency AsyncDifficulty 1
In C#, what does the await keyword do when applied to a Task?
- aBlocks the calling thread until the task completes
- bSuspends the method without blocking the thread until the task completes✓
- cImmediately cancels the task if it has not started yet
- dConverts the task into a synchronous method call
Explanation:await builds a state machine: the rest of the method is captured as a continuation, the calling thread is freed, and once the task completes the continuation resumes (usually on a thread-pool thread). The thread is never blocked — this is the key difference from .Result/.Wait().
Csharp Concurrency AsyncDifficulty 1
Why can't the caller of an async void method await its completion?
- aBecause
async void methods always run synchronously - bBecause
async void methods return no value - cBecause
async void methods are not allowed to contain await expressions - dBecause
async void gives the caller no Task to hold and await✓
Explanation:An async Task method returns a Task handle the caller can await, observe exceptions on, or compose with WhenAll/WhenAny. An async void method returns nothing at all — there is no handle, so the caller has no way to know when it finished or whether it threw.
Csharp Concurrency AsyncDifficulty 1
Which of the following statements about the C# lock statement is correct?
- aA reference-type object is required as the lock monitor; a value type causes a compile error✓
- b
lock can be used directly on any value type, including int or a struct - c
lock only works inside methods marked async - d
lock automatically releases its monitor once the containing class is garbage collected
Explanation:lock (obj) requires obj to be a reference type because the monitor is tied to the object's identity (its sync block on the heap). Passing a value type is a compile-time error (CS0185), since boxing would create a new object each time and the lock would never actually synchronize anything.
Csharp Concurrency AsyncDifficulty 1
What does passing CancellationToken.None to an async method mean?
- aThe operation is guaranteed to run on a background thread
- bThe method throws immediately because no token was supplied
- cThe method receives a token that can never be canceled✓
- dThe default token cancels the operation after a fixed timeout
Explanation:CancellationToken.None returns a real, valid CancellationToken value whose IsCancellationRequested is always false and which is never signaled. It's the safe placeholder to pass when there's genuinely no cancellation source, and it costs nothing extra to check.
Csharp Concurrency AsyncDifficulty 1
What is Task.CompletedTask typically used for?
- aTo start a brand-new task on the thread pool
- bTo cancel every currently running task in the application
- cTo block the calling thread until all pending tasks finish
- dAn already-completed
Task singleton, useful to satisfy a Task-returning signature✓
Explanation:Task.CompletedTask is a cached, already-completed Task singleton. It's commonly returned from a method that implements an interface requiring Task but has nothing asynchronous to do in a given branch, avoiding an unnecessary allocation.
Csharp Concurrency AsyncDifficulty 1
The C# lock (obj) { ... } statement is essentially syntactic sugar for which underlying pattern?
- a
Interlocked.Increment(ref obj) wrapped around the block - b
Monitor.Enter(obj) plus a try/finally that calls Monitor.Exit(obj)✓ - c
SemaphoreSlim.Wait() called with a timeout of zero - d
Task.Run(() => obj) wrapping the block's body
Explanation:The compiler lowers lock (obj) { body } into Monitor.Enter(obj); try { body } finally { Monitor.Exit(obj); } (with the two-argument Enter overload for reliability). The finally guarantees the monitor is released even if the body throws.