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
Which class in C# is used to read characters from a file?
A) FileReader
B) FileStream
C) StreamReader
D) StreamWriter
Which method is used to write text to a file in C#?
A) WriteToFile
B) Write
C) WriteLine
D) StreamWriter.Write
What type of stream is used for reading binary data from a file?
A) FileStream
B) MemoryStream
C) BinaryReader
D) TextReader
To read a line from a file, which method of StreamReader is used?
A) ReadToEnd
B) ReadLine
C) ReadByte
D) ReadChar
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
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
What is the default file access mode for a FileStream?
A) ReadWrite
B) WriteOnly
C) Read
D) Append
Which method in FileStream writes data to a file?
A) WriteBytes
B) Write
C) Flush
D) Close
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
Which of the following classes is not derived from Stream?
A) MemoryStream
B) FileStream
C) StreamReader
D) StringWriter
3. Directory and Path Operations
Which class in C# allows you to manipulate file and directory paths?
A) Path
B) File
C) DirectoryInfo
D) FileInfo
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
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
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
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
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
In StreamReader, which method is used to read all content from a file?
A) ReadAllBytes
B) Read
C) ReadLine
D) ReadToEnd
What is the default encoding used by StreamWriter in C#?
A) UTF-8
B) ASCII
C) Unicode
D) UTF-16
Which method in StreamWriter is used to write a line of text to a file?
A) WriteLine
B) Write
C) WriteText
D) WriteString
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
Which attribute marks a class for serialization in C#?
A) [Serializable]
B) [DataContract]
C) [SerializableClass]
D) [DataMember]
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
Which method is used to deserialize an object from a file in C#?
A) Deserialize()
B) DeserializeObject()
C) BinaryFormatter.Deserialize()
D) File.Deserialize()
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.
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
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
Which stream is typically used for binary serialization?
A) FileStream
B) MemoryStream
C) NetworkStream
D) BinaryWriter
Which of these formats is used by the BinaryFormatter for serialization?
A) XML
B) JSON
C) Binary
D) Text
How can you deserialize an object from a MemoryStream?
A) MemoryStream.Deserialize()
B) BinaryFormatter.Deserialize()
C) StreamReader.ReadObject()
D) FileStream.DeserializeObject()
Which of these classes cannot be used for serialization in C#?
A) BinaryFormatter
B) SoapFormatter
C) XmlSerializer
D) FileStream
Answer Key
Qno
Answer (Option with Text)
1
C) StreamReader
2
B) Write
3
A) FileStream
4
B) ReadLine
5
A) ReadAllBytes
6
B) Open, read, and write to a file
7
C) Read
8
B) Write
9
B) To get or set the current position in the file
10
D) StringWriter
11
A) Path
12
B) Directory.CreateDirectory
13
B) To check if a directory exists
14
A) Path.Combine
15
B) Path.GetExtension
16
D) Both B and C
17
D) ReadToEnd
18
A) UTF-8
19
A) WriteLine
20
C) Both A and B
21
A) [Serializable]
22
B) Converting a byte stream back to an object
23
C) BinaryFormatter.Deserialize()
24
B) It supports object serialization and deserialization
25
B) Serializes objects into XML format
26
B) It marks a property or field as not to be serialized