Microsoft Certified Solutions Developer (MCSD) Certification Practice Test

Disable ads (and more) with a membership for a one time $2.99 payment

Prepare for the Microsoft Certified Solutions Developer (MCSD) Certification Exam. Practice with flashcards and multiple choice questions. Master each topic with hints and insights. Achieve your certification goals!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


How can a Task signal that it has been canceled?

  1. By returning a cancellation status

  2. By throwing an InvalidOperationException

  3. By using the CancellationToken to throw an OperationCanceledException

  4. By notifying listeners through events

The correct answer is: By using the CancellationToken to throw an OperationCanceledException

A Task can signal that it has been canceled by using the CancellationToken to throw an OperationCanceledException. This is the standard approach in the .NET framework for indicating that an operation has been canceled. When a task is designed to handle cancellation, it checks the state of the CancellationToken associated with it. If the token has been signaled (indicating that cancellation is requested), the task should throw an OperationCanceledException. This exception is specifically meant to represent that the operation was canceled, allowing the consuming code to handle it appropriately. This pattern encourages cleaner error handling, as the OperationCanceledException can be caught and interpreted explicitly within the context of task cancellation, rather than conflating it with other types of exceptions that may indicate programming errors or unexpected conditions. The other options do not align with the established practice for task cancellation in .NET. For example, returning a cancellation status lacks the granularity required for exception handling, while throwing an InvalidOperationException typically indicates a misuse of the API rather than a cancellation scenario. Additionally, relying solely on event notifications does not provide the direct mechanism needed for task cancellation status reporting within the task's execution context.