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

Async file reading and writing in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async File IO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of async file read with ReadAllTextAsync
What is the output of this C# program that reads a file asynchronously?
C Sharp (C#)
using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        string path = "test.txt";
        await File.WriteAllTextAsync(path, "Hello Async World!");
        string content = await File.ReadAllTextAsync(path);
        Console.WriteLine(content);
    }
}
ACompilation error
BSystem.Threading.Tasks.Task`1[System.String]
CEmpty output
DHello Async World!
Attempts:
2 left
💡 Hint
Remember that ReadAllTextAsync returns the file content as a string asynchronously.
Predict Output
intermediate
1:30remaining
Result of async file write with WriteAllTextAsync
What will be the content of the file "output.txt" after running this program?
C Sharp (C#)
using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        string path = "output.txt";
        await File.WriteAllTextAsync(path, "Async Write Test");
    }
}
AAsync Write Test
BFile is empty
CFile contains "System.Threading.Tasks.Task"
DCompilation error
Attempts:
2 left
💡 Hint
WriteAllTextAsync writes the string content to the file asynchronously.
🔧 Debug
advanced
2:00remaining
Identify the error in async file reading code
What error will this code produce when run?
C Sharp (C#)
using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
    static void Main()
    {
        string content = File.ReadAllTextAsync("file.txt").Result;
        Console.WriteLine(content);
    }
}
ACompilation error due to missing async keyword
BDeadlock or blocking on async code
CFileNotFoundException if file.txt does not exist
DNo error, prints file content
Attempts:
2 left
💡 Hint
Blocking on async code with .Result can cause deadlocks in some contexts.
Predict Output
advanced
2:00remaining
Output of async file read with partial buffer
What is the output of this program that reads a file asynchronously using a buffer?
C Sharp (C#)
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        string path = "buffer.txt";
        await File.WriteAllTextAsync(path, "Buffer Read Test");

        using FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
        byte[] buffer = new byte[6];
        int bytesRead = await fs.ReadAsync(buffer, 0, buffer.Length);
        string text = Encoding.UTF8.GetString(buffer, 0, bytesRead);
        Console.WriteLine(text);
    }
}
ABuffer Read
BBuffer Read Test
CBuffer
DreffuB
Attempts:
2 left
💡 Hint
The buffer size is 6 bytes, so only the first 6 characters are read.
🧠 Conceptual
expert
1:30remaining
Why use async file IO in C#?
Which is the best reason to use async file reading and writing in C# applications?
ATo improve UI responsiveness by not blocking the main thread during file operations
BTo make file operations faster by using multiple CPU cores automatically
CTo guarantee file operations never fail due to IO errors
DTo reduce the file size when writing asynchronously
Attempts:
2 left
💡 Hint
Think about what happens when a program waits for a file operation on the main thread.