Preview Questions

8.7. Preview Questions#

Complete these questions before class to prepare for the chapter. Use the dropdowns to check your answers.

8.7.1. True or False#

1. StreamReader is used to write data to a file.

Answer

FalseStreamReader reads; use StreamWriter to write.

2. You should close a file stream after you are done using it.

Answer

True — Unclosed streams can cause resource leaks or data loss.

3. File.ReadAllText() reads the entire file into a single string.

Answer

True — It returns all file content as one string.

4. Regular expressions can only match exact strings, not patterns.

Answer

False — Regex is designed to match patterns, not just literal strings.

5. The using statement ensures a resource is disposed when the block exits.

Answer

Trueusing calls Dispose() automatically, even on exceptions.

8.7.2. Multiple Choice#

1. Which C# class reads text line by line from a file?

a) FileWriter
b) StreamReader
c) FileInput
d) TextGetter

Answer

b) StreamReaderStreamReader wraps a file and exposes ReadLine() and ReadToEnd().

2. Which namespace contains file I/O classes in C#?

a) System.IO
b) System.File
c) System.Text
d) System.Storage

Answer

a) System.IOSystem.IO contains StreamReader, StreamWriter, File, etc.

3. What does StreamWriter.WriteLine(text) do?

a) Reads a line
b) Writes text followed by a newline
c) Deletes a line
d) Counts lines in a file

Answer

b) Writes text followed by a newline — It writes the string and appends the platform’s newline character.

4. What does File.Exists(path) return if the file is not found?

a) null
b) 0
c) false
d) throws an exception

Answer

c) falseFile.Exists() returns false for missing files — it never throws.

5. In a regular expression, what does the pattern \d+ match?

a) Any letter
b) One or more digits
c) Whitespace
d) Any character

Answer

b) One or more digits\d matches a digit; + means one or more.

Footnotes