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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Writing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this C# code writing to a file?

Consider the following C# code that writes text to a file and then reads it back. What will be printed on the console?

C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string path = "testfile.txt";
        File.WriteAllText(path, "Hello World!");
        string content = File.ReadAllText(path);
        Console.WriteLine(content);
    }
}
AFile not found exception
Btestfile.txt
CHello World!
DCompilation error
Attempts:
2 left
💡 Hint

Think about what File.WriteAllText and File.ReadAllText do.

Predict Output
intermediate
2:00remaining
What happens if you try to write to a file in a non-existent directory?

What will happen when running this C# code?

C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string path = "nonexistent_dir/output.txt";
        File.WriteAllText(path, "Data");
        Console.WriteLine("Done");
    }
}
ADone
BCompilation error
CFileNotFoundException
DDirectoryNotFoundException
Attempts:
2 left
💡 Hint

Does File.WriteAllText create missing directories automatically?

🔧 Debug
advanced
3:00remaining
Why does this code throw an exception when writing to a file?

The following code throws an exception. Identify the cause.

C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string path = "output.txt";
        using (StreamWriter writer = new StreamWriter(path)) {
            writer.WriteLine("Line 1");
        }
        writer.WriteLine("Line 2");
    }
}
AThe file path is invalid causing a FileNotFoundException
BThe StreamWriter is used outside its using block causing an ObjectDisposedException
CThe StreamWriter constructor throws an IOException
DNo exception, code runs fine
Attempts:
2 left
💡 Hint

Check where the writer variable is accessible.

🧠 Conceptual
advanced
1:30remaining
Which method appends text to an existing file without overwriting?

Which of the following C# methods appends text to a file instead of overwriting it?

AFile.AppendAllText(path, text)
BFile.WriteAllText(path, text)
CFile.ReadAllText(path)
DFile.Create(path)
Attempts:
2 left
💡 Hint

Look for the method name that suggests adding to the end.

Predict Output
expert
3:00remaining
What is the content of the file after running this code?

Given the code below, what will be the content of log.txt after execution?

C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string path = "log.txt";
        File.WriteAllText(path, "Start\n");
        using (StreamWriter sw = new StreamWriter(path, append: true)) {
            sw.WriteLine("Middle");
        }
        File.WriteAllText(path, "End");
    }
}
AEnd
BStart\nMiddle\nEnd
CStart\nEnd
DStart\nMiddle
Attempts:
2 left
💡 Hint

Consider the order of file writes and whether content is overwritten.