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

Task.WhenAny for first completion in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Task.WhenAny Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Task.WhenAny with different delays
What will be the output of this C# code snippet using Task.WhenAny to await the first completed task?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var task1 = Task.Delay(300).ContinueWith(_ => "Task1");
        var task2 = Task.Delay(100).ContinueWith(_ => "Task2");
        var task3 = Task.Delay(200).ContinueWith(_ => "Task3");

        var firstCompleted = await Task.WhenAny(task1, task2, task3);
        Console.WriteLine(await firstCompleted);
    }
}
ATask1
BTask2
CTask3
DCompilation error
Attempts:
2 left
💡 Hint
Think about which task finishes first based on the delay times.
🧠 Conceptual
intermediate
1:30remaining
Understanding Task.WhenAny behavior
Which statement best describes what Task.WhenAny does in C#?
AIt cancels all other tasks once the first task completes.
BIt waits for all tasks to complete and returns their results as a list.
CIt returns the first task that completes, regardless of success or failure.
DIt throws an exception if any task fails before completion.
Attempts:
2 left
💡 Hint
Consider what happens if one task completes with an error before others finish.
🔧 Debug
advanced
2:00remaining
Identify the runtime error with Task.WhenAny usage
What runtime error will this code produce when executed?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        Task<string> task1 = Task.FromResult("Hello");
        Task<string> task2 = null;

        var first = await Task.WhenAny(task1, task2);
        Console.WriteLine(await first);
    }
}
AArgumentNullException
BNullReferenceException
CInvalidOperationException
DNo error, outputs "Hello"
Attempts:
2 left
💡 Hint
Check what happens if one of the tasks passed to Task.WhenAny is null.
Predict Output
advanced
2:00remaining
Output when first completed task faults
What will be the output of this code snippet?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        var task1 = Task.Run(() => throw new InvalidOperationException("Error in task1"));
        var task2 = Task.Delay(100).ContinueWith(_ => "Success");

        var first = await Task.WhenAny(task1, task2);

        try
        {
            Console.WriteLine(await first);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.GetType().Name);
        }
    }
}
ASuccess
BCompilation error
CTaskCanceledException
DInvalidOperationException
Attempts:
2 left
💡 Hint
Consider which task completes first and what happens when awaiting a faulted task.
🚀 Application
expert
3:00remaining
Using Task.WhenAny to implement a timeout
Which code snippet correctly uses Task.WhenAny to implement a 500ms timeout for an asynchronous operation DoWorkAsync()? The code should print "Timeout" if the operation takes too long, or print the result if it completes in time.
A
var workTask = DoWorkAsync();
var timeoutTask = Task.Delay(500);
var first = await Task.WhenAny(workTask, timeoutTask);
if (first == timeoutTask) Console.WriteLine("Timeout");
else Console.WriteLine(await workTask);
B
var workTask = DoWorkAsync();
var timeoutTask = Task.Delay(500);
var first = await Task.WhenAny(workTask, timeoutTask);
if (first == workTask) Console.WriteLine("Timeout");
else Console.WriteLine(await timeoutTask);
C
var workTask = DoWorkAsync();
var timeoutTask = Task.Delay(500);
await Task.WhenAny(workTask, timeoutTask);
if (workTask.IsCompleted) Console.WriteLine("Timeout");
else Console.WriteLine(await workTask);
D
var workTask = DoWorkAsync();
var timeoutTask = Task.Delay(500);
var first = await Task.WhenAny(workTask, timeoutTask);
if (first == timeoutTask) Console.WriteLine(await timeoutTask);
else Console.WriteLine("Timeout");
Attempts:
2 left
💡 Hint
Check which task finished first and print accordingly.