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

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

Choose your learning style9 modes available
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.