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

Using statement with file streams in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Stream Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of reading file lines with using statement
What is the output of this C# code snippet when the file 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);
            }
        }
    }
}
ANo output, throws exception
BLine1 Line2 Line3
CLine1\nLine2\nLine3
DLine3\nLine2\nLine1
Attempts:
2 left
💡 Hint
Think about how StreamReader.ReadLine() reads lines and how Console.WriteLine outputs them.
Predict Output
intermediate
2: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");
        }
    }
}
AHello\nWorld\n
BHello World
CHelloWorld
DFile is empty
Attempts:
2 left
💡 Hint
Remember that WriteLine adds a new line after the text.
Predict Output
advanced
2: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");
        }
    }
}
AThrows IOException
BStart\nEnd\n
CStart
DEmpty
Attempts:
2 left
💡 Hint
Think about when the StreamWriter flushes data to the file and when the StreamReader reads it.
Predict Output
advanced
2: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());
            }
        }
    }
}
AEmpty
BLine1\n
CThrows IOException
DLine1
Attempts:
2 left
💡 Hint
Consider what Flush() does to the StreamWriter buffer.
🧠 Conceptual
expert
2: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#?
AIt automatically closes and disposes the file stream to free resources and avoid file locks.
BIt improves the speed of file reading and writing by caching data.
CIt allows multiple threads to access the file stream simultaneously without errors.
DIt encrypts the file content automatically for security.
Attempts:
2 left
💡 Hint
Think about resource management and what happens if files are not closed properly.