Challenge - 5 Problems
File Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of reading a text file line by line
What will be the output of this C# code snippet that reads a text file line by line and prints each line?
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { string path = "test.txt"; File.WriteAllText(path, "Hello\nWorld\nCSharp"); using StreamReader reader = new StreamReader(path); string? line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } }
Attempts:
2 left
💡 Hint
Remember that ReadLine reads one line at a time and Console.WriteLine prints each line on a new line.
✗ Incorrect
The code writes three lines to the file, then reads each line and prints it. Each line is printed on its own line, so the output is three lines: Hello, World, and CSharp.
❓ Predict Output
intermediate2:00remaining
Reading entire file content at once
What will this C# code print when reading the entire content of a text file at once?
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { string path = "data.txt"; File.WriteAllText(path, "Line1\nLine2\nLine3"); string content = File.ReadAllText(path); Console.WriteLine(content); } }
Attempts:
2 left
💡 Hint
ReadAllText reads the whole file as one string including newlines.
✗ Incorrect
The file contains three lines separated by newline characters. ReadAllText returns the entire content including newlines, so printing it shows three lines.
❓ Predict Output
advanced2:00remaining
Reading file with incorrect path
What happens when this C# code tries to read a file that does not exist?
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { string path = "nofile.txt"; string content = File.ReadAllText(path); Console.WriteLine(content); } }
Attempts:
2 left
💡 Hint
What happens if you try to read a file that does not exist?
✗ Incorrect
File.ReadAllText throws a FileNotFoundException if the file path does not exist.
❓ Predict Output
advanced2:00remaining
Reading file lines with LINQ
What is the output of this C# code that reads all lines from a file and filters lines containing 'a'?
C Sharp (C#)
using System; using System.IO; using System.Linq; class Program { static void Main() { string path = "words.txt"; File.WriteAllLines(path, new[] {"apple", "banana", "cherry", "date"}); var lines = File.ReadLines(path).Where(line => line.Contains('a')); foreach (var line in lines) { Console.WriteLine(line); } } }
Attempts:
2 left
💡 Hint
Check which words contain the letter 'a'.
✗ Incorrect
The words 'apple', 'banana', and 'date' contain 'a'. 'cherry' does not. So only those three lines are printed.
❓ Predict Output
expert2:00remaining
Reading file asynchronously
What will this C# async code print when reading a file asynchronously?
C Sharp (C#)
using System; using System.IO; using System.Threading.Tasks; class Program { static async Task Main() { string path = "async.txt"; await File.WriteAllTextAsync(path, "Async\nRead\nTest"); string content = await File.ReadAllTextAsync(path); Console.WriteLine(content); } }
Attempts:
2 left
💡 Hint
Async file methods read and write the full content including newlines.
✗ Incorrect
The file is written with three lines separated by newlines. Reading asynchronously returns the full content including newlines, so printing it shows three lines.