Challenge - 5 Problems
Stream Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } } }
Attempts:
2 left
💡 Hint
ReadLine reads one line at a time, stopping at newline characters.
✗ Incorrect
The ReadLine method reads one line at a time. The first call reads "Hello", the second reads "World". Console.WriteLine adds a newline after each, so the output is Hello\nWorld\n.
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
WriteLine adds a newline after each line.
✗ Incorrect
StreamWriter.WriteLine writes the string and adds a newline. ReadToEnd reads the whole file including newlines. So output includes both lines separated by newlines.
❓ Predict Output
advanced2: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); } } }
Attempts:
2 left
💡 Hint
Read reads one character, ReadLine reads until newline or end.
✗ Incorrect
Read reads 'a' (first char). Then ReadLine reads the rest of the first line after 'a', which is 'bc'. The newline is consumed by ReadLine but not included in output.
❓ Predict Output
advanced2: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())); } } } }
Attempts:
2 left
💡 Hint
AutoFlush controls when data is sent to the underlying buffer.
✗ Incorrect
With AutoFlush false, Write does not immediately update the underlying stream, so first print is empty. After Flush(), the stream contains "Hello".
🧠 Conceptual
expert2:00remaining
StreamReader disposal and file locking
Which option correctly describes what happens if you forget to dispose a StreamReader after reading a file?
Attempts:
2 left
💡 Hint
Think about resource management and file locks in Windows.
✗ Incorrect
If StreamReader is not disposed, the underlying file handle remains open, locking the file. Other processes cannot access it until disposal or program exit.