Sample questions
Question 1Medium
Which of the following class to use to read content of a file?
- StreamWriter
- StreamReader
- FileRead
- ReadFile
Answer: B. StreamReader
Explanation
StreamReader reads text from a file and is in System.IO. Wrap it in a using block so that it is closed and disposed after use. StreamWriter writes text, and FileRead and ReadFile are not classes.
Step-by-step solution
- StreamReader is the System.IO class used to read text files.
- StreamWriter writes text to a file; it does not read it.
- FileRead and ReadFile are not classes in System.IO.
- So the answer is option B, StreamReader.
Why the other options are wrong
- A. StreamWriter writes to files; it does not read them.
- C. FileRead is not a class in System.IO.
- D. ReadFile is not a class in System.IO.
Tip: Read with StreamReader and write with StreamWriter; both are in System.IO and work well with using.
Question 2Medium
Which method of StreamReader class reads the complete file in one function call?
- ReadLine
- ReadCompleteFile
- ReadToEnd
- None of the above
Answer: C. ReadToEnd
Explanation
ReadToEnd reads all characters from the current position to the end of the stream in one call. ReadLine reads only one line at a time, and ReadCompleteFile does not exist.
Step-by-step solution
- ReadToEnd reads all characters from current position to end of stream.
- It is designed for reading entire file in one call.
- Other methods like ReadLine read line by line.
- ReadCompleteFile is not a valid method in StreamReader class.
- Thus, the correct answer is ReadToEnd.
Why the other options are wrong
- A. ReadLine reads line by line, not entire file.
- B. ReadCompleteFile is not a valid method in StreamReader.
Tip: Use ReadToEnd for quick file reading, but be cautious with streams that don't have a clear end.
Question 3Medium
Which namespaces provide access to classes that represents the file handle architecture in c# ?
- System.ADO
- System.File
- System.IO
- System.FileHandling
Answer: C. System.IO
Explanation
System.IO is the namespace with classes for reading and writing files and streams, such as File and FileStream.
Step-by-step solution
- System.IO namespace provides classes for file handling in C#.
- These classes allow reading, writing, and managing files and streams.
- System.File and System.ADO are not related to file handles.
- System.FileHandling does not exist in .NET framework.
- The correct namespace is System.IO.
Why the other options are wrong
- A. ADO is for database access, not file handling.
- B. No such namespace exists in .NET framework.
- D. No such namespace exists in .NET framework.
Tip: Remember System.IO for file handling in C#.
Question 4Medium
Which class to use to Write content to a File?
- StreamReader
- StreamWriter
- FileWriter
- FileOpener
Answer: B. StreamWriter
Explanation
StreamWriter writes text to a file and is in System.IO. Writing it inside a using block closes and disposes it when done, and Flush pushes buffered data to the file. StreamReader only reads, so it is the wrong choice.
Step-by-step solution
- The task is to write text to a file.
- StreamWriter is the .NET class for writing text to a file.
- So the answer is StreamWriter: option B.
Why the other options are wrong
- A. StreamReader reads from a stream; it does not write.
- C. FileWriter is a Java class, not a .NET class.
- D. FileOpener is not a .NET class for writing file content.
Tip: Reader reads, Writer writes; wrap a writer in using so the file is closed.
Question 5Medium
What is the return type of File.ReadAllLines(../../file.txt")?
- String
- StringBuilder
- String[]
- char[]
Answer: C. String[]
Explanation
ReadAllLines opens a text file, reads all lines of the file, and then closes the file. It return an array of string i.e. string[]. The string array contains all of the files.
Step-by-step solution
- File.ReadAllLines opens a text file and reads all of its lines.
- It returns one entry per line, collected in an array.
- Each entry is a string, so the return type is string[].
- So the answer is option C, String[].
Why the other options are wrong
- A. String would be a single string, not one entry per line.
- B. StringBuilder is not returned by ReadAllLines.
- D. char[] would be an array of single characters, not of lines.
Tip: File.ReadAllText returns one string; File.ReadAllLines returns string[].
Question 6Medium
Which class to use to get the detailed information of a file path?
- Path
- FilePath
- File
- Volume
Answer: A. Path
Explanation
Path class performs operation on System.String instance that contain file or directory path information. These operations areperformed in a cross-platform manner. A file stored at a path. The .NET framework provides effective ways to deal with filename and paths. Itintroduces the path type in the System.IO namespaces. There arecomplexities when dealing with paths. You will often need to extract parts of filename paths. The .NET Framework team has considered this problem.path class is ideal.
For Example
String directory = path.GetDirectoryName("D:\\TestSolution\\file.txt");
Step-by-step solution
- Path class handles file and directory paths in a cross-platform way.
- It provides methods like GetDirectoryName to extract parts of a path.
- String directory = Path.GetDirectoryName("D:\\TestSolution\\file.txt");
- This shows how Path works with file paths directly.
- Other options do not handle detailed path operations as effectively.
- Final answer: A (Path).
Why the other options are wrong
- B. FilePath is not a standard class in .NET for handling paths.
- C. File class deals with file operations but not detailed path info.
Tip: Use the Path class for all file path-related operations in .NET.
Question 7Medium
Can we use ReadAllLines to count number of lines in a file?
int fileLineCount = File.ReadAllLines("../../file.txt").Length;
Console.WriteLine(fileLineCount);
- Yes
- No
Answer: A. Yes
Explanation
Read All Lines opens a text file, reads all lines of the file, and then closes the file. It returns an array of string i.e. string[].
This string array contains all lines of the file. So Property length will give the number of lines in the file.
Step-by-step solution
- File.ReadAllLines reads every line of the file into a string array.
- Each array element holds one line, so the array length is the number of lines.
- Taking the Length gives the line count, so the answer is Yes, option A.
Why the other options are wrong
- B. ReadAllLines returns one string per line, so its Length counts the lines; No is wrong.
Tip: To count lines, read them into an array and use its Length.
Question 8Medium
Which of the following class provides method to Copy, Delete, Move ,Open,
Replace a File?
I. File
II. Fileinfo
III. Directory
IV. DirectoryInfo
- I
- I and II
- III and IV
- I and III
Answer: B. I and II
Explanation
Both File and FileInfo can copy, delete, move, open and replace files. File uses static methods, while FileInfo works on a file object. Directory and DirectoryInfo work on folders, not single files.
Step-by-step solution
- File class provides methods like Copy, Delete, Move, Open and Replace.
- FileInfo class also has similar methods for file operations.
- Directory class is used for folder operations only.
- DirectoryInfo class is used for folder operations only.
- So I and II provide the required methods.
Why the other options are wrong
- A. Only File class is mentioned, but FileInfo also provides these methods.
- C. Directory and DirectoryInfo are for folders, not files.
Tip: Remember that both File and FileInfo classes handle file operations.
Practise all 8 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →