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

Using statement with file streams in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a file stream using a using statement.

C Sharp (C#)
using (var fileStream = new FileStream("example.txt", [1]))
{
    // Read or write operations
}
Drag options to blanks, or click blank then click option'
AFileAccess.Read
BFileMode.Open
CFileShare.Read
DFileOptions.None
Attempts:
3 left
💡 Hint
Common Mistakes
Using FileAccess instead of FileMode as the second argument.
Using FileShare or FileOptions in place of FileMode.
2fill in blank
medium

Complete the code to write text to a file using a StreamWriter inside a using statement.

C Sharp (C#)
using (var writer = new StreamWriter(new FileStream("output.txt", FileMode.Create)))
{
    writer.[1]("Hello, world!");
}
Drag options to blanks, or click blank then click option'
AReadLine
BClose
CWriteLine
DFlush
Attempts:
3 left
💡 Hint
Common Mistakes
Using ReadLine which reads input instead of writing.
Calling Close explicitly inside using block (not needed).
3fill in blank
hard

Fix the error in the using statement to properly dispose the FileStream after reading.

C Sharp (C#)
FileStream fs = new FileStream("data.txt", FileMode.Open);
using ([1])
{
    // Read from fs
}
Drag options to blanks, or click blank then click option'
Afs
Bnew FileStream("data.txt", FileMode.Open)
CFileStream fs
DStreamReader fs
Attempts:
3 left
💡 Hint
Common Mistakes
Creating a new FileStream inside using instead of using the existing one.
Trying to declare the variable again inside using.
4fill in blank
hard

Fill both blanks to create a using statement that reads all text from a file.

C Sharp (C#)
using (var reader = new StreamReader(new FileStream("file.txt", [1])))
{
    string content = reader.[2]();
}
Drag options to blanks, or click blank then click option'
AFileMode.Open
BReadToEnd
CReadLine
DFileMode.Create
Attempts:
3 left
💡 Hint
Common Mistakes
Using FileMode.Create which overwrites the file.
Using ReadLine which reads only one line.
5fill in blank
hard

Fill all three blanks to write multiple lines to a file using a using statement and a loop.

C Sharp (C#)
using (var writer = new StreamWriter(new FileStream("log.txt", [1])))
{
    foreach (var line in lines)
    {
        writer.[2](line);
    }
    writer.[3]();
}
Drag options to blanks, or click blank then click option'
AFileMode.Create
BWriteLine
CFlush
DFileMode.Append
Attempts:
3 left
💡 Hint
Common Mistakes
Using FileMode.Create which erases existing content.
Not calling Flush, causing data not to be saved immediately.