Bird
Raised Fist0
C Sharp (C#)programming~20 mins

StreamReader and StreamWriter in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Stream Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Reading lines with StreamReader
What is the output of this C# code snippet?
C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string text = "Hello\nWorld";
        using (var reader = new StringReader(text)) {
            string line1 = reader.ReadLine();
            string line2 = reader.ReadLine();
            Console.WriteLine(line1);
            Console.WriteLine(line2);
        }
    }
}
AHello\nWorld
BHello\nWorld\nHello
CHello\nWorld\n
DHello\nWorld\nnull
Attempts:
2 left
💡 Hint
ReadLine reads one line at a time, stopping at newline characters.
Predict Output
intermediate
2:00remaining
Writing and reading text with StreamWriter and StreamReader
What will be printed by this C# program?
C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string path = "test.txt";
        using (var writer = new StreamWriter(path)) {
            writer.WriteLine("Line1");
            writer.WriteLine("Line2");
        }
        using (var reader = new StreamReader(path)) {
            string content = reader.ReadToEnd();
            Console.Write(content);
        }
        File.Delete(path);
    }
}
ALine1\nLine2\n
BLine1Line2
CLine1\nLine2
DLine1\nLine2\nnull
Attempts:
2 left
💡 Hint
WriteLine adds a newline after each line.
Predict Output
advanced
2:00remaining
StreamReader Read vs ReadLine behavior
What is the output of this C# code?
C Sharp (C#)
using System;
using System.IO;

class Program {
    static void Main() {
        string text = "abc\ndef";
        using (var reader = new StringReader(text)) {
            int firstChar = reader.Read();
            string line = reader.ReadLine();
            Console.WriteLine((char)firstChar);
            Console.WriteLine(line);
        }
    }
}
Aa\nbc
Ba\ndef
Ca\ncdef
Da\nb
Attempts:
2 left
💡 Hint
Read reads one character, ReadLine reads until newline or end.
Predict Output
advanced
2:00remaining
StreamWriter AutoFlush effect
What will this program print?
C Sharp (C#)
using System;
using System.IO;
using System.Text;

class Program {
    static void Main() {
        using (var ms = new MemoryStream()) {
            using (var writer = new StreamWriter(ms)) {
                writer.AutoFlush = false;
                writer.Write("Hello");
                Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
                writer.Flush();
                Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
            }
        }
    }
}
AHello\nHello
BolleHn\olleH
C\n\nHello
D\nHello
Attempts:
2 left
💡 Hint
AutoFlush controls when data is sent to the underlying buffer.
🧠 Conceptual
expert
2:00remaining
StreamReader disposal and file locking
Which option correctly describes what happens if you forget to dispose a StreamReader after reading a file?
AThe file is immediately unlocked and can be accessed by other processes even if StreamReader is not disposed.
BThe file remains locked and cannot be accessed by other processes until the program ends or garbage collection runs.
CThe StreamReader automatically disposes itself after reading the file, so no locking occurs.
DThe file is deleted automatically when StreamReader is not disposed.
Attempts:
2 left
💡 Hint
Think about resource management and file locks in Windows.

Practice

(1/5)
1. What is the primary purpose of the StreamReader class in C#?
easy
A. To read text from a file
B. To write text to a file
C. To create a new file
D. To delete a file

Solution

  1. Step 1: Understand StreamReader's role

    StreamReader is designed to read text data from files.
  2. Step 2: Differentiate from StreamWriter

    StreamWriter writes text, not reads it.
  3. Final Answer:

    To read text from a file -> Option A
  4. Quick Check:

    StreamReader reads files = D [OK]
Hint: StreamReader reads, StreamWriter writes [OK]
Common Mistakes:
  • Confusing StreamReader with StreamWriter
  • Thinking StreamReader creates files
  • Assuming StreamReader deletes files
2. Which of the following is the correct syntax to open a file for writing using StreamWriter in C#?
easy
A. StreamWriter writer = StreamWriter("file.txt");
B. using (StreamWriter writer = new StreamWriter("file.txt")) { }
C. using StreamWriter writer = new StreamWriter("file.txt");
D. StreamWriter writer = new StreamWriter.read("file.txt");

Solution

  1. Step 1: Recognize correct StreamWriter instantiation

    The correct way is to use new StreamWriter("file.txt") inside a using block for safe disposal.
  2. Step 2: Check syntax correctness

    using (StreamWriter writer = new StreamWriter("file.txt")) { } uses using with proper syntax and constructor call.
  3. Final Answer:

    using (StreamWriter writer = new StreamWriter("file.txt")) { } -> Option B
  4. Quick Check:

    Correct StreamWriter syntax = B [OK]
Hint: Use 'using' with new StreamWriter(filename) [OK]
Common Mistakes:
  • Missing 'new' keyword
  • Not using 'using' block for disposal
  • Incorrect method calls like .read() on StreamWriter
3. What will be the output of the following C# code snippet?
using (var writer = new StreamWriter("test.txt")) {
    writer.WriteLine("Hello");
    writer.WriteLine("World");
}
using (var reader = new StreamReader("test.txt")) {
    string content = reader.ReadToEnd();
    Console.Write(content);
}
medium
A. HelloWorld
B. Hello\nWorld
C. Hello World
D. Hello World

Solution

  1. Step 1: Understand StreamWriter.WriteLine behavior

    Each WriteLine writes the string plus a newline character at the end.
  2. Step 2: ReadToEnd reads full content including newlines

    The reader reads the entire file content, preserving newlines.
  3. Final Answer:

    Hello\nWorld\n -> Option C
  4. Quick Check:

    WriteLine adds newline, ReadToEnd reads all [OK]
Hint: WriteLine adds newline; ReadToEnd reads full text [OK]
Common Mistakes:
  • Ignoring newline characters added by WriteLine
  • Assuming WriteLine writes without newlines
  • Confusing output formatting in Console.Write
4. Identify the error in the following code snippet:
StreamReader reader = new StreamReader("data.txt");
string line = reader.ReadLine();
Console.WriteLine(line);
reader.Close();
medium
A. Missing 'using' block to ensure file closure
B. ReadLine() should be ReadAll()
C. StreamReader cannot read text files
D. reader.Close() should be called before ReadLine()

Solution

  1. Step 1: Check resource management

    The code opens a StreamReader but does not use a using block, risking resource leaks if exceptions occur.
  2. Step 2: Confirm method correctness

    ReadLine() is correct to read one line; Close() is called but manual closing is less safe than using.
  3. Final Answer:

    Missing 'using' block to ensure file closure -> Option A
  4. Quick Check:

    Use 'using' to auto-close files [OK]
Hint: Always use 'using' to auto-close streams [OK]
Common Mistakes:
  • Not using 'using' block for automatic disposal
  • Confusing ReadLine with ReadAll
  • Calling Close before reading
5. You want to copy the contents of one text file to another using StreamReader and StreamWriter. Which code snippet correctly performs this task?
hard
A. using (var reader = new StreamReader("source.txt")) { string content = reader.ReadToEnd(); var writer = new StreamWriter("dest.txt"); writer.Write(content); }
B. using (var writer = new StreamWriter("dest.txt")) { using (var reader = new StreamReader("source.txt")) { string line; while ((line = reader.ReadLine()) != null) { writer.WriteLine(line); } } }
C. var reader = new StreamReader("source.txt"); var writer = new StreamWriter("dest.txt"); string line = reader.ReadLine(); while (line != null) { writer.WriteLine(line); line = reader.ReadLine(); } reader.Close(); writer.Close();
D. using (var reader = new StreamReader("source.txt")) { using (var writer = new StreamWriter("dest.txt")) { string line; while ((line = reader.ReadLine()) != null) { writer.WriteLine(line); } } }

Solution

  1. Step 1: Check proper resource management

    using (var reader = new StreamReader("source.txt")) { using (var writer = new StreamWriter("dest.txt")) { string line; while ((line = reader.ReadLine()) != null) { writer.WriteLine(line); } } } uses nested using blocks to ensure both reader and writer are properly closed.
  2. Step 2: Verify reading and writing logic

    It reads line by line until null, writing each line to the destination file correctly.
  3. Final Answer:

    Correct nested using blocks with line-by-line copy -> Option D
  4. Quick Check:

    Nested using + line loop = A [OK]
Hint: Use nested 'using' blocks and loop ReadLine [OK]
Common Mistakes:
  • Not disposing writer properly
  • Reversing reader and writer order in using blocks
  • Not looping to read all lines
  • Not disposing writer in option D