Java MCQs – File handling and streams

Java Multiple Choice Questions (MCQs) focused on File Handling and Streams are a fantastic tool for interview preparation, especially for those aiming to land Java programming jobs. These MCQs cover various topics related to working with files and streams in Java. They test your knowledge of concepts like reading and writing data, managing files, and dealing with data streams.

Practicing with these MCQs not only helps you assess your understanding but also improves your problem-solving skills. Whether you’re applying for a junior or senior Java developer position, mastering file handling and streams is crucial, and these MCQs provide a structured way to evaluate and enhance your skills in this important area.

1.) What is the primary purpose of input streams in Java?

A) To write data to a file.
B) To read data from a file.
C) To append data to a file.
D) To create directories.

Answer: Option B

Explanation: Input streams are used to read data from a file or another source.

2.) Which class in Java is used to create a new directory?

A) FileReader
B) FileWriter
C) File
D) Directory

Answer: Option C

Explanation: The File class is used to create directories and manipulate files.

3.) What is the purpose of the FileOutputStream class in Java?

A) To write binary data to a file.
B) To read binary data from a file.
C) To create a new file.
D) To append text to a file.

Answer: Option A

Explanation: FileOutputStream is used to write binary data to a file.

4.) Which method is used to check if a file or directory exists in Java?

A) exists()
B) createNewFile()
C) makeDirectory()
D) isFileOrDirectory()

Answer: Option A

Explanation: The exists() method checks if a file or directory exists.

5.) What is the purpose of the FileReader class in Java?

A) To read binary data from a file.
B) To write binary data to a file.
C) To read character data from a file.
D) To write character data to a file.

Answer: Option C

Explanation: FileReader is used to read character data from a file.

6.) Which class in Java is used to read data line by line from a file?

A) BufferedReader
B) FileInputStream
C) FileWriter
D) OutputStream

Answer: Option A

Explanation: BufferedReader is commonly used to read data line by line from a file.

7.) What is the purpose of the File.separator constant in Java?

A) To separate text within a file.
B) To separate directories and files in file paths.
C) To define the end of a file.
D) To concatenate strings in file operations.

Answer: Option B

Explanation: File.separator is used to separate directories and files in file paths, ensuring platform independence.

8.) Which class is used for efficient reading of characters from a file in Java?

A) FileReader
B) FileInputStream
C) BufferedReader
D) DataInputStream

Answer: Option C

Explanation: BufferedReader is used for efficient character reading from a file.

9.) What does the FileWriter class in Java do?

A) Reads binary data from a file.
B) Appends text to an existing file.
C) Deletes a file.
D) Creates a new directory.

Answer: Option B

Explanation: FileWriter is used to append text to an existing file.

10.) What is the primary purpose of the BufferedOutputStream class in Java?

A) To write binary data to a file efficiently.
B) To read binary data from a file efficiently.
C) To create a new directory.
D) To read character data from a file.

Answer: Option A

Explanation: BufferedOutputStream is used for efficient writing of binary data to a file.

Leave a Reply

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