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

Reading text files in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Reading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
        }
    }
}
A
Hello
World
CSharp
BHello World CSharp
CHelloWorldCSharp
DThrows an exception
Attempts:
2 left
💡 Hint
Remember that ReadLine reads one line at a time and Console.WriteLine prints each line on a new line.
Predict Output
intermediate
2: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);
    }
}
ALine1 Line2 Line3
B
Line1
Line2
Line3
CLine1Line2Line3
DThrows an exception
Attempts:
2 left
💡 Hint
ReadAllText reads the whole file as one string including newlines.
Predict Output
advanced
2: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);
    }
}
AThrows NullReferenceException
BPrints null
CPrints empty string
DThrows FileNotFoundException
Attempts:
2 left
💡 Hint
What happens if you try to read a file that does not exist?
Predict Output
advanced
2: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);
        }
    }
}
A
apple
banana
date
B
banana
date
Ccherry
D
apple
banana
cherry
date
Attempts:
2 left
💡 Hint
Check which words contain the letter 'a'.
Predict Output
expert
2: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);
    }
}
ACompilation error
BAsync Read Test
C
Async
Read
Test
DAsyncReadTest
Attempts:
2 left
💡 Hint
Async file methods read and write the full content including newlines.