Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the file name in quotes.
Using a method name instead of a file name.
✗ Incorrect
The file name must be a string in quotes to open it correctly.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using reading methods instead of writing.
Not using quotes around the file name.
✗ Incorrect
To write to a file, provide the file name as a string to StreamWriter.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using write methods instead of read methods.
Confusing ReadAllText with ReadAllLines.
✗ Incorrect
Use ReadAllLines to read all lines from a file into a string array.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers as keys instead of strings.
Using strings for lengths instead of integers.
✗ Incorrect
The key is a word string and the value is its length as an integer.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ReadAllText instead of ReadAllLines.
Using wrong method to count lines.
Using wrong length value.
✗ Incorrect
ReadAllLines reads all lines, Count counts lines matching the condition, and 5 is the length threshold.