Challenge - 5 Problems
Async File IO Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate1: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); } }
Attempts:
2 left
💡 Hint
Remember that ReadAllTextAsync returns the file content as a string asynchronously.
✗ Incorrect
The program writes "Hello Async World!" to the file asynchronously, then reads it back asynchronously and prints the content. So the output is exactly the string written.
❓ Predict Output
intermediate1: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"); } }
Attempts:
2 left
💡 Hint
WriteAllTextAsync writes the string content to the file asynchronously.
✗ Incorrect
The program writes the string "Async Write Test" to the file asynchronously, so the file content will be exactly that string.
🔧 Debug
advanced2: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); } }
Attempts:
2 left
💡 Hint
Blocking on async code with .Result can cause deadlocks in some contexts.
✗ Incorrect
Calling .Result on an async method in a synchronous Main can cause a deadlock because the async method waits for the synchronization context to be free, which is blocked by .Result waiting synchronously.
❓ Predict Output
advanced2: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); } }
Attempts:
2 left
💡 Hint
The buffer size is 6 bytes, so only the first 6 characters are read.
✗ Incorrect
The program writes "Buffer Read Test" to the file, then reads 6 bytes asynchronously. The first 6 characters are "Buffer" (6 letters), but since the buffer is size 6, it reads exactly 6 bytes which correspond to "Buffer". The output is "Buffer".
🧠 Conceptual
expert1:30remaining
Why use async file IO in C#?
Which is the best reason to use async file reading and writing in C# applications?
Attempts:
2 left
💡 Hint
Think about what happens when a program waits for a file operation on the main thread.
✗ Incorrect
Async file IO allows the program to continue running other code while waiting for the file operation to complete, which keeps the UI responsive and avoids freezing.