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

Why file operations matter in C Sharp (C#) - Test Your Understanding

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

Complete the code to open a file for reading.

C Sharp (C#)
using System.IO;

var file = new StreamReader([1]);
Drag options to blanks, or click blank then click option'
AStreamReader
Bdata.txt
CFile.OpenRead
D"data.txt"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the file name in quotes.
Using a method name instead of a file name.
2fill in blank
medium

Complete the code to write text to a file.

C Sharp (C#)
using System.IO;

using var writer = new StreamWriter([1]);
writer.WriteLine("Hello World");
Drag options to blanks, or click blank then click option'
A"output.txt"
BConsole.Out
CFile.ReadAllText
DStreamReader
Attempts:
3 left
💡 Hint
Common Mistakes
Using reading methods instead of writing.
Not using quotes around the file name.
3fill in blank
hard

Fix the error in the code to read all lines from a file.

C Sharp (C#)
string[] lines = File.[1]("log.txt");
Drag options to blanks, or click blank then click option'
AReadAllLines
BWriteAllLines
CWriteAllText
DOpenWrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using write methods instead of read methods.
Confusing ReadAllText with ReadAllLines.
4fill in blank
hard

Fill both blanks to create a dictionary with word lengths for words longer than 3 characters.

C Sharp (C#)
var lengths = new Dictionary<string, int> { { [1], [2] } };
Drag options to blanks, or click blank then click option'
A"example"
B7
C"word"
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers as keys instead of strings.
Using strings for lengths instead of integers.
5fill in blank
hard

Fill all three blanks to read a file, count lines longer than 5 characters, and print the count.

C Sharp (C#)
var lines = File.[1]("data.txt");
int count = lines.[2](line => line.Length > [3]);
Console.WriteLine(count);
Drag options to blanks, or click blank then click option'
AReadAllLines
BCount
C5
DReadAllText
Attempts:
3 left
💡 Hint
Common Mistakes
Using ReadAllText instead of ReadAllLines.
Using wrong method to count lines.
Using wrong length value.