0
0
C Sharp (C#)programming~20 mins

Task.WhenAll for parallel execution in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Task.WhenAll Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Task.WhenAll example?

Consider the following C# code using Task.WhenAll to run tasks in parallel. What will be printed to the console?

C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var task1 = Task.Delay(100).ContinueWith(_ => 1);
        var task2 = Task.Delay(50).ContinueWith(_ => 2);
        var results = await Task.WhenAll(task1, task2);
        Console.WriteLine(string.Join(",", results));
    }
}
ARuntime exception
B2,1
CCompilation error
D1,2
Attempts:
2 left
💡 Hint

Remember that Task.WhenAll returns results in the order of the tasks passed, not the order they complete.

🧠 Conceptual
intermediate
1:30remaining
Which statement about Task.WhenAll is true?

Choose the correct statement about Task.WhenAll in C#.

AIt waits for all tasks to complete and returns their results in the order tasks were passed.
BIt returns results as soon as the first task completes.
CIt cancels all tasks if any one task fails.
DIt runs tasks sequentially, one after another.
Attempts:
2 left
💡 Hint

Think about how Task.WhenAll handles multiple tasks and their results.

🔧 Debug
advanced
2:30remaining
Why does this Task.WhenAll code throw an exception?

Examine the code below. It throws an exception at runtime. What is the cause?

C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var task1 = Task.FromException<int>(new InvalidOperationException("Error in task1"));
        var task2 = Task.Run(() => 42);
        var results = await Task.WhenAll(task1, task2);
        Console.WriteLine(string.Join(",", results));
    }
}
AThe code has a syntax error causing compilation failure.
BTask.WhenAll returns partial results ignoring failed tasks.
CTask.WhenAll throws AggregateException because one task failed.
DTask.WhenAll cancels all tasks when one fails, so no exception is thrown.
Attempts:
2 left
💡 Hint

Think about how exceptions in tasks are handled when using Task.WhenAll.

📝 Syntax
advanced
2:00remaining
Which option correctly uses Task.WhenAll with async lambdas?

Identify the correct syntax to run two async lambdas in parallel and await their results using Task.WhenAll.

Avar results = await Task.WhenAll(Task.Run(async () => await Task.Delay(10)), Task.Run(async () => await Task.Delay(20)));
Bvar results = await Task.WhenAll(async () => await Task.Delay(10), async () => await Task.Delay(20));
Cvar results = await Task.WhenAll(() => Task.Delay(10), () => Task.Delay(20));
Dvar results = Task.WhenAll(Task.Delay(10), Task.Delay(20));
Attempts:
2 left
💡 Hint

Remember that Task.WhenAll expects tasks, not delegates or lambdas directly.

🚀 Application
expert
2:30remaining
How many items are in the results array after this Task.WhenAll call?

Given the code below, how many elements does the results array contain after Task.WhenAll completes?

C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        Task<int> t1 = Task.FromResult(5);
        Task<int> t2 = Task.Run(() => 10);
        Task<int> t3 = Task.Delay(100).ContinueWith(_ => 15);
        var results = await Task.WhenAll(t1, t2, t3);
        Console.WriteLine(results.Length);
    }
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint

Count how many tasks are passed to Task.WhenAll.