Challenge - 5 Problems
File Stream Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of reading file lines with using statement
What is the output of this C# code snippet when the file
Line1
Line2
Line3?
test.txt contains three lines: Line1
Line2
Line3?
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { using (var reader = new StreamReader("test.txt")) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } } }
Attempts:
2 left
💡 Hint
Think about how StreamReader.ReadLine() reads lines and how Console.WriteLine outputs them.
✗ Incorrect
The code reads each line from the file in order and prints it on its own line. So the output matches the file lines exactly.
❓ Predict Output
intermediate2:00remaining
Output of writing to a file with using statement
What will be the content of
output.txt after running this C# code?C Sharp (C#)
using System; using System.IO; class Program { static void Main() { using (var writer = new StreamWriter("output.txt")) { writer.WriteLine("Hello"); writer.WriteLine("World"); } } }
Attempts:
2 left
💡 Hint
Remember that WriteLine adds a new line after the text.
✗ Incorrect
Each WriteLine writes the string and then a newline, so the file contains two lines: Hello and World.
❓ Predict Output
advanced2:00remaining
Behavior of nested using statements with file streams
What is the output of this C# program?
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { using (var writer = new StreamWriter("nested.txt")) { writer.WriteLine("Start"); using (var reader = new StreamReader("nested.txt")) { string content = reader.ReadToEnd(); Console.WriteLine(string.IsNullOrEmpty(content) ? "Empty" : content); } writer.WriteLine("End"); } } }
Attempts:
2 left
💡 Hint
Think about when the StreamWriter flushes data to the file and when the StreamReader reads it.
✗ Incorrect
The StreamWriter buffers data and does not flush before the StreamReader reads, so the file is empty at that moment.
❓ Predict Output
advanced2:00remaining
Output of using statement with file stream and manual flush
What will this program print?
C Sharp (C#)
using System; using System.IO; class Program { static void Main() { using (var writer = new StreamWriter("flush.txt")) { writer.WriteLine("Line1"); writer.Flush(); using (var reader = new StreamReader("flush.txt")) { Console.WriteLine(reader.ReadToEnd()); } } } }
Attempts:
2 left
💡 Hint
Consider what Flush() does to the StreamWriter buffer.
✗ Incorrect
Flush() forces the buffered data to be written to the file, so the StreamReader reads the content correctly.
🧠 Conceptual
expert2:00remaining
Why is using statement important with file streams?
Which of the following best explains why the using statement is important when working with file streams in C#?
Attempts:
2 left
💡 Hint
Think about resource management and what happens if files are not closed properly.
✗ Incorrect
The using statement ensures the file stream is closed and disposed even if exceptions occur, preventing resource leaks and file locks.