C# File Handling MCQ

1. Which namespace is commonly used for file handling in C#?

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

Answer:

a) System.IO

Explanation:

The System.IO namespace contains types for handling files and data streams, making it the primary namespace used for file handling in C#.

2. How do you create a text file in C#?

a) File.CreateText("filename.txt")
b) File.New("filename.txt")
c) File.Create("filename.txt")
d) FileStream.Create("filename.txt")

Answer:

a) File.CreateText("filename.txt")

Explanation:

The File.CreateText method is used to create a new text file in C#. It returns a StreamWriter.

3. What class is used to read text from a file in C#?

a) FileReader
b) File.Read
c) StreamReader
d) TextReader

Answer:

c) StreamReader

Explanation:

StreamReader is a class in the System.IO namespace used for reading text from files.

4. Which method is used to write text to a file in C#?

a) File.WriteText()
b) StreamWriter.Write()
c) File.AppendText()
d) File.WriteAllLines()

Answer:

d) File.WriteAllLines()

Explanation:

File.WriteAllLines() writes a string array to a file, with each string as a line. For appending or continuous writing, StreamWriter can be used.

5. How do you append text to an existing file in C#?

a) File.Append("filename.txt", "text")
b) File.AppendText("filename.txt")
c) StreamWriter.Append("filename.txt", "text")
d) File.AppendAllText("filename.txt", "text")

Answer:

d) File.AppendAllText("filename.txt", "text")

Explanation:

File.AppendAllText method appends text to the end of an existing file.

6. What is the purpose of the FileStream class in C#?

a) To stream data to memory
b) To read and write to a network stream
c) To read from and write to files
d) To serialize objects

Answer:

c) To read from and write to files

Explanation:

FileStream is used for reading from and writing to files as streams of bytes, providing control over byte positions in files.

7. Which method checks for the existence of a file in C#?

a) File.Exists("filename.txt")
b) File.Check("filename.txt")
c) File.IsPresent("filename.txt")
d) Directory.Exists("filename.txt")

Answer:

a) File.Exists("filename.txt")

Explanation:

File.Exists method is used to check if a file exists at a specified path.

8. How do you copy a file in C#?

a) File.CopyTo("source.txt", "destination.txt")
b) File.Copy("source.txt", "destination.txt")
c) FileStream.Copy("source.txt", "destination.txt")
d) StreamWriter.Copy("source.txt", "destination.txt")

Answer:

b) File.Copy("source.txt", "destination.txt")

Explanation:

The File.Copy method is used to copy a file to a new location.

9. How do you delete a file in C#?

a) File.Delete("filename.txt")
b) File.Remove("filename.txt")
c) File.Erase("filename.txt")
d) FileStream.Delete("filename.txt")

Answer:

a) File.Delete("filename.txt")

Explanation:

File.Delete method is used to delete a specified file.

10. Which class is used to read and write binary files in C#?

a) BinaryReader and BinaryWriter
b) FileStream
c) StreamReader and StreamWriter
d) BufferedStream

Answer:

a) BinaryReader and BinaryWriter

Explanation:

BinaryReader and BinaryWriter are used for reading and writing binary data to and from files.

11. How do you move a file in C#?

a) File.MoveTo("source.txt", "destination.txt")
b) File.Move("source.txt", "destination.txt")
c) FileStream.Move("source.txt", "destination.txt")
d) StreamWriter.Move("source.txt", "destination.txt")

Answer:

b) File.Move("source.txt", "destination.txt")

Explanation:

The File.Move method is used to move a file to a new location.

12. What is the use of the Path class in C#?

a) To handle file paths and directories
b) To create new paths in the file system
c) To encrypt file paths
d) To stream file paths

Answer:

a) To handle file paths and directories

Explanation:

The Path class in the System.IO namespace provides methods for manipulating file and directory paths.

13. What exception is commonly associated with file handling errors in C#?

a) IOException
b) FileNotFoundException
c) FileAccessException
d) PathException

Answer:

a) IOException

Explanation:

IOException is a general exception that occurs when an I/O error happens during file handling operations.

14. How can you read all lines of a text file into a string array in C#?

a) File.ReadAllLines("filename.txt")
b) StreamReader.ReadAllLines("filename.txt")
c) FileStream.ReadAllLines("filename.txt")
d) File.ReadLines("filename.txt")

Answer:

a) File.ReadAllLines("filename.txt")

Explanation:

File.ReadAllLines reads all the lines of the specified file into a string array.

15. Which method is used to read all text from a file in one go in C#?

a) File.ReadAllText("filename.txt")
b) StreamReader.ReadToEnd("filename.txt")
c) FileStream.ReadAll("filename.txt")
d) File.ReadAll("filename.txt")

Answer:

a) File.ReadAllText("filename.txt")

Explanation:

File.ReadAllText reads all text from the file and returns it as a single string.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top