Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Returning values from async methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Async Return Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of async method returning Task
What is the output of this C# program when run?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static async Task<int> GetNumberAsync() {
        await Task.Delay(100);
        return 42;
    }

    static async Task Main() {
        int result = await GetNumberAsync();
        Console.WriteLine(result);
    }
}
A42
BTask`1
C0
DCompilation error
Attempts:
2 left
💡 Hint
Remember that awaiting an async method unwraps the Task and gives the actual return value.
❓ Predict Output
intermediate
2:00remaining
Return type of async method without Task
What happens when you try to compile and run this code?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static async Task<int> GetNumberAsync() {
        await Task.Delay(100);
        return 10;
    }

    static async Task Main() {
        int result = await GetNumberAsync();
        Console.WriteLine(result);
    }
}
ACompilation error
BRuntime exception
CPrints 10
DPrints Task`1
Attempts:
2 left
💡 Hint
Async methods must return Task, Task, or void (rarely).
🔧 Debug
advanced
2:00remaining
Why does this async method return null?
Consider this code snippet. Why does the output print "Result is: " with no number after it?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static async Task<string> GetTextAsync() {
        await Task.Delay(50);
        string text = null;
        return text;
    }

    static async Task Main() {
        string result = await GetTextAsync();
        Console.WriteLine($"Result is: {result}");
    }
}
AThe method throws an exception causing no output.
BThe await is missing so result is null.
CThe method returns null string, so nothing is printed after the colon.
DThe Console.WriteLine is incorrect and does not print the variable.
Attempts:
2 left
💡 Hint
Check what value the async method returns and how null strings print.
❓ Predict Output
advanced
2:00remaining
Output of async method returning Task without await
What is the output of this program?
C Sharp (C#)
using System;
using System.Threading.Tasks;

class Program {
    static async Task DoWorkAsync() {
        Console.WriteLine("Start");
        // No await here
        Console.WriteLine("End");
    }

    static async Task Main() {
        await DoWorkAsync();
    }
}
ACompilation error
BStart\nEnd
CEnd
DStart
Attempts:
2 left
💡 Hint
Async methods can run synchronously if no await is present.
🧠 Conceptual
expert
2:00remaining
What is the type of the value returned by an async method with signature 'async Task'?
Given an async method declared as 'async Task ComputeAsync()', what is the type of the value you get when you await this method?
Avoid
BTask<int>
CTask
Dint
Attempts:
2 left
💡 Hint
Await unwraps the Task and gives the inner value.

Practice

(1/5)
1. What type should an async method return if it needs to return a value of type int?
easy
A. void
B. Task
C. int
D. Task<int>

Solution

  1. Step 1: Understand async method return types

    Async methods that return a value must return Task<T> where T is the value type.
  2. Step 2: Match the return type to the value type

    Since the method returns an int, the return type should be Task<int>.
  3. Final Answer:

    Task<int> -> Option D
  4. Quick Check:

    Async method returning int uses Task<int> [OK]
Hint: Async methods returning values use Task<T> [OK]
Common Mistakes:
  • Using void for async methods that return values
  • Returning int directly from async method
  • Using Task without generic type for returning values
2. Which of the following is the correct syntax to declare an async method that returns a string value?
easy
A. async Task<string> GetNameAsync()
B. string async Task GetNameAsync()"
C. Task async string GetNameAsync()
D. async string GetNameAsync()

Solution

  1. Step 1: Identify correct async method syntax

    An async method returning a value must have the signature: async Task<T> MethodName().
  2. Step 2: Check each option

    async Task<string> GetNameAsync() matches the correct syntax: async Task<string> GetNameAsync(). Others have wrong order or missing Task<T>.
  3. Final Answer:

    async Task<string> GetNameAsync() -> Option A
  4. Quick Check:

    Async method returning string uses async Task<string> [OK]
Hint: Async method syntax: async Task<T> MethodName() [OK]
Common Mistakes:
  • Placing async after return type
  • Using string directly without Task<>
  • Mixing order of async and Task keywords
3. What will be the output of the following code?
async Task<int> GetNumberAsync() { return 5; }

async Task Main()
{
    int result = await GetNumberAsync();
    Console.WriteLine(result);
}
medium
A. Task<int>
B. 5
C. Compilation error
D. 0

Solution

  1. Step 1: Understand async method return and await

    The method GetNumberAsync returns a Task<int> with value 5. Await unwraps the value.
  2. Step 2: Check what is printed

    The awaited result is 5, so Console.WriteLine(result) prints 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Await unwraps Task<int> to int value 5 [OK]
Hint: Await unwraps Task<T> to get the value [OK]
Common Mistakes:
  • Printing Task<int> instead of awaited int
  • Expecting compilation error due to async
  • Assuming default int value 0 is printed
4. Identify the error in this async method:
async Task<int> CalculateAsync()
{
    int value = 10;
    return Task.FromResult(value);
}
medium
A. No error, code is correct
B. Method should return int, not Task<int>
C. Missing await keyword before Task.FromResult
D. Returning Task.FromResult inside async method causes nested Task

Solution

  1. Step 1: Analyze return statement in async method

    Async methods can return Task<T> directly, such as Task.FromResult(value), which is a common and valid pattern for completed tasks.
  2. Step 2: Verify the code runs correctly

    The async state machine properly handles the returned Task<int>, producing a single Task<int> with value 10 for the caller. No nesting or errors occur.
  3. Final Answer:

    No error, code is correct -> Option A
  4. Quick Check:

    Task.FromResult is valid in async Task<int> methods [OK]
Hint: Task.FromResult(value) is valid in async Task<T> methods [OK]
Common Mistakes:
  • Misunderstanding that Task.FromResult causes nested Tasks
  • Thinking async methods must return T directly, not Task<T>
  • Believing await is required before Task.FromResult
5. You want to write an async method that fetches user data and returns the user's name as a string. The fetching is simulated by a delay. Which of the following implementations correctly returns the name without blocking the caller?
hard
A. async string GetUserNameAsync() { await Task.Delay(1000); return \"Alice\"; }
B. Task<string> GetUserNameAsync() { return \"Alice\"; }
C. async Task<string> GetUserNameAsync() { await Task.Delay(1000); return \"Alice\"; }
D. async Task GetUserNameAsync() { await Task.Delay(1000); return \"Alice\"; }"

Solution

  1. Step 1: Check method signature and return type

    The method must be async and return Task<string> to return a string asynchronously.
  2. Step 2: Verify method body and return statement

    async Task<string> GetUserNameAsync() { await Task.Delay(1000); return \"Alice\"; } uses await Task.Delay(1000) to simulate delay and returns the string correctly. Others have wrong return types or missing async.
  3. Final Answer:

    async Task<string> GetUserNameAsync() { await Task.Delay(1000); return "Alice"; } -> Option C
  4. Quick Check:

    Async method returning string uses async Task<string> with await [OK]
Hint: Async method returning value uses async Task<T> with await [OK]
Common Mistakes:
  • Returning string directly without Task
  • Using async with wrong return type
  • Missing await causing blocking
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard