Challenge - 5 Problems
Task Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple Task returning a string
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() { Task<string> task = Task.FromResult("Hello World"); string result = await task; Console.WriteLine(result); } }
Attempts:
2 left
💡 Hint
Remember that Task.FromResult creates a completed Task with the given result.
✗ Incorrect
The Task.FromResult method returns a completed Task with the specified result. Awaiting it returns the string "Hello World", which is printed.
❓ Predict Output
intermediate2:00remaining
Value of Task after delay
What will be the output of this program?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task Main() { Task<int> task = GetNumberAsync(); int value = await task; Console.WriteLine(value); } static async Task<int> GetNumberAsync() { await Task.Delay(100); return 42; } }
Attempts:
2 left
💡 Hint
The method returns 42 after a short delay.
✗ Incorrect
The async method GetNumberAsync returns 42 after awaiting a delay. Awaiting the Task returns 42, which is printed.
❓ Predict Output
advanced2:00remaining
What happens when awaiting a non-generic Task?
Consider this code snippet. What is printed when it runs?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task Main() { Task task = Task.Delay(50); await task; Console.WriteLine("Done"); } }
Attempts:
2 left
💡 Hint
Awaiting a Task waits for its completion but returns no value.
✗ Incorrect
Awaiting a non-generic Task waits for it to complete. After the delay, "Done" is printed.
🧠 Conceptual
advanced2:00remaining
Difference between Task and Task
Which statement correctly describes the difference between Task and Task in C#?
Attempts:
2 left
💡 Hint
Think about whether the operation returns a result or not.
✗ Incorrect
Task is for operations that do not return a value. Task returns a value of type T when awaited.
🔧 Debug
expert2:00remaining
Identify the runtime error in this Task usage
What error will this code produce when run?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static void Main() { Task<int> task = Task.Run(() => 10 / 0); Console.WriteLine(task.Result); } }
Attempts:
2 left
💡 Hint
Exceptions inside Tasks are wrapped when accessing Result.
✗ Incorrect
The division by zero inside the Task causes a DivideByZeroException, but accessing task.Result throws an AggregateException wrapping it.