Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to run two tasks in parallel and wait for both to finish.
C Sharp (C#)
var task1 = Task.Run(() => Console.WriteLine("Task 1")); var task2 = Task.Run(() => Console.WriteLine("Task 2")); await [1](task1, task2);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.WaitAll which is synchronous and blocks the thread.
Using Task.WaitAny which waits for only one task to complete.
✗ Incorrect
Task.WhenAll runs multiple tasks in parallel and returns a task that completes when all have finished.
2fill in blank
mediumComplete the code to start three tasks and wait for all to complete using Task.WhenAll.
C Sharp (C#)
var tasks = new List<Task> {
Task.Run(() => DoWork(1)),
Task.Run(() => DoWork(2)),
Task.Run(() => DoWork(3))
};
await [1](tasks); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the list to Task.WaitAll which is synchronous.
Using Task.WaitAny which waits for only one task.
✗ Incorrect
Task.WhenAll accepts a collection of tasks and returns a task that completes when all tasks finish.
3fill in blank
hardFix the error in the code to correctly wait for multiple tasks to complete asynchronously.
C Sharp (C#)
var taskA = Task.Run(() => Compute(10)); var taskB = Task.Run(() => Compute(20)); Task.WhenAll(taskA, taskB).Wait(); // Fix this line Console.WriteLine("All tasks done");
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.WaitAll which blocks the thread.
Using Task.WhenAny which waits for only one task.
✗ Incorrect
Using 'await Task.WhenAll' waits asynchronously without blocking the thread, unlike '.Wait()' which blocks.
4fill in blank
hardFill both blanks to create a dictionary with task results after all tasks complete.
C Sharp (C#)
var tasks = new List<Task<int>> {
Task.Run(() => 1),
Task.Run(() => 2),
Task.Run(() => 3)
};
var results = await [1](tasks);
var dict = results.[2]((result, index) => new { index, result }).ToDictionary(x => x.index, x => x.result); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using SelectMany which flattens collections incorrectly.
Using a non-existent method SelectIndexed.
✗ Incorrect
Task.WhenAll waits for all tasks and returns results; Select projects each result with its index.
5fill in blank
hardFill in the blanks to run tasks in parallel, filter results greater than 5, and create a list of those results.
C Sharp (C#)
var tasks = new List<Task<int>> {
Task.Run(() => 3),
Task.Run(() => 7),
Task.Run(() => 10)
};
var results = await [1](tasks);
var filtered = results.Where(x => x [2] 5).ToList();
Console.WriteLine(string.Join(", ", filtered)); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Task.WaitAll which blocks the thread instead of awaiting.
Using '<' which filters results less than 5.
✗ Incorrect
Task.WhenAll waits for all tasks and returns an array of results; '>' filters results greater than 5.