MCQs on File I/O and Streams | C#

This collection of 30 multiple-choice questions (MCQs) focuses on essential concepts of File I/O, Streams, and Serialization in C#. It covers topics like file reading and writing, directory operations, and stream handling. These MCQs are designed to test your understanding of C# file handling, including StreamReader, StreamWriter, and serialization techniques.


1. File I/O and Streams

  1. Which class in C# is used to read characters from a file?
    • A) FileReader
    • B) FileStream
    • C) StreamReader
    • D) StreamWriter
  2. Which method is used to write text to a file in C#?
    • A) WriteToFile
    • B) Write
    • C) WriteLine
    • D) StreamWriter.Write
  3. What type of stream is used for reading binary data from a file?
    • A) FileStream
    • B) MemoryStream
    • C) BinaryReader
    • D) TextReader
  4. To read a line from a file, which method of StreamReader is used?
    • A) ReadToEnd
    • B) ReadLine
    • C) ReadByte
    • D) ReadChar
  5. Which method allows reading all bytes from a file at once in C#?
    • A) ReadAllBytes
    • B) ReadFile
    • C) ReadBytes
    • D) File.Read

2. File Streams

  1. What does FileStream allow you to do?
    • A) Write data to a memory stream
    • B) Open, read, and write to a file
    • C) Compress file data
    • D) Serialize objects
  2. What is the default file access mode for a FileStream?
    • A) ReadWrite
    • B) WriteOnly
    • C) Read
    • D) Append
  3. Which method in FileStream writes data to a file?
    • A) WriteBytes
    • B) Write
    • C) Flush
    • D) Close
  4. What is the purpose of the FileStream.Position property?
    • A) To specify the buffer size
    • B) To get or set the current position in the file
    • C) To check if the file exists
    • D) To read the next byte in the stream
  5. Which of the following classes is not derived from Stream?
    • A) MemoryStream
    • B) FileStream
    • C) StreamReader
    • D) StringWriter

3. Directory and Path Operations

  1. Which class in C# allows you to manipulate file and directory paths?
    • A) Path
    • B) File
    • C) DirectoryInfo
    • D) FileInfo
  2. How can you create a directory in C# if it doesn’t already exist?
    • A) Directory.Create
    • B) Directory.CreateDirectory
    • C) File.CreateDirectory
    • D) Path.Create
  3. What is the purpose of Directory.Exists method?
    • A) To check if a directory is writable
    • B) To check if a directory exists
    • C) To check the contents of a directory
    • D) To list files in a directory
  4. Which method in the Path class is used to combine two paths into one?
    • A) Path.Combine
    • B) Path.Add
    • C) Path.Merge
    • D) Path.Join
  5. To get the file extension from a path, which method is used?
    • A) Path.GetFileExtension
    • B) Path.GetExtension
    • C) Path.GetName
    • D) Path.GetFileName

4. StreamReader and StreamWriter Classes

  1. Which of the following is a correct way to instantiate a StreamWriter?
    • A) StreamWriter writer = new StreamWriter();
    • B) StreamWriter writer = new StreamWriter(“file.txt”);
    • C) StreamWriter writer = new StreamWriter(File.Open(“file.txt”));
    • D) Both B and C
  2. In StreamReader, which method is used to read all content from a file?
    • A) ReadAllBytes
    • B) Read
    • C) ReadLine
    • D) ReadToEnd
  3. What is the default encoding used by StreamWriter in C#?
    • A) UTF-8
    • B) ASCII
    • C) Unicode
    • D) UTF-16
  4. Which method in StreamWriter is used to write a line of text to a file?
    • A) WriteLine
    • B) Write
    • C) WriteText
    • D) WriteString
  5. How do you close a StreamReader or StreamWriter in C#?
    • A) Close()
    • B) Dispose()
    • C) Both A and B
    • D) None of the above

5. Serialization and Deserialization

  1. Which attribute marks a class for serialization in C#?
    • A) [Serializable]
    • B) [DataContract]
    • C) [SerializableClass]
    • D) [DataMember]
  2. What is deserialization in C#?
    • A) Converting an object to a byte stream
    • B) Converting a byte stream back to an object
    • C) Saving an object to a file
    • D) Encrypting object data
  3. Which method is used to deserialize an object from a file in C#?
    • A) Deserialize()
    • B) DeserializeObject()
    • C) BinaryFormatter.Deserialize()
    • D) File.Deserialize()
  4. Which of the following is true about the BinaryFormatter class?
    • A) It is used for encrypting objects.
    • B) It supports object serialization and deserialization.
    • C) It can only serialize primitive types.
    • D) It is deprecated and not recommended for use.
  5. What does the XmlSerializer class do in C#?
    • A) Serializes objects into binary format
    • B) Serializes objects into XML format
    • C) Decrypts serialized objects
    • D) Writes data to files in XML
  6. What is the role of the [NonSerialized] attribute?
    • A) It marks the class as non-serializable
    • B) It marks a property or field as not to be serialized
    • C) It indicates a class cannot be deserialized
    • D) It is used to encrypt fields during serialization
  7. Which stream is typically used for binary serialization?
    • A) FileStream
    • B) MemoryStream
    • C) NetworkStream
    • D) BinaryWriter
  8. Which of these formats is used by the BinaryFormatter for serialization?
    • A) XML
    • B) JSON
    • C) Binary
    • D) Text
  9. How can you deserialize an object from a MemoryStream?
    • A) MemoryStream.Deserialize()
    • B) BinaryFormatter.Deserialize()
    • C) StreamReader.ReadObject()
    • D) FileStream.DeserializeObject()
  10. Which of these classes cannot be used for serialization in C#?
    • A) BinaryFormatter
    • B) SoapFormatter
    • C) XmlSerializer
    • D) FileStream

Answer Key

QnoAnswer (Option with Text)
1C) StreamReader
2B) Write
3A) FileStream
4B) ReadLine
5A) ReadAllBytes
6B) Open, read, and write to a file
7C) Read
8B) Write
9B) To get or set the current position in the file
10D) StringWriter
11A) Path
12B) Directory.CreateDirectory
13B) To check if a directory exists
14A) Path.Combine
15B) Path.GetExtension
16D) Both B and C
17D) ReadToEnd
18A) UTF-8
19A) WriteLine
20C) Both A and B
21A) [Serializable]
22B) Converting a byte stream back to an object
23C) BinaryFormatter.Deserialize()
24B) It supports object serialization and deserialization
25B) Serializes objects into XML format
26B) It marks a property or field as not to be serialized
27A) FileStream
28C) Binary
29B) BinaryFormatter.Deserialize()
30D) FileStream

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top