Java MCQs – File handling and streams

11.) Which class is used to write serialized objects to a file in Java?

A) ObjectInputStream
B) FileOutputStream
C) ObjectOutputStream
D) FileWriter

Answer: Option C

Explanation: ObjectOutputStream is used to write serialized objects to a file.

12.) In Java, which class is used for reading and writing character data to a file in a platform-independent way?

A) FileReader
B) FileWriter
C) BufferedReader
D) PrintWriter

Answer: Option D

Explanation: PrintWriter is used for reading and writing character data in a platform-independent way.

13.) What does the File.delete() method do in Java?

A) Delete a file.
B) Appends text to an existing file.
C) Deletes a file or directory.
D) Delete binary data from a file.

Answer: Option C

Explanation: The File.delete() method is used to delete a file or directory.

14.) What does the File.renameTo() method do in Java?

A) Renames a file or directory.
B) Renames only a file.
C) Renames only a directory.
D) None of these.

Answer: Option A

Explanation: The File.renameTo() method renames a file or directory.

15.) Which method is used to close a file stream in Java?

A) close()
B) flush()
C) closeStream()
D) exit()

Answer: Option A

Explanation: The close() method is used to close a file stream.

16.) What is the primary purpose of the RandomAccessFile class in Java?

A) To read data from a file.
B) To write data to a file.
C) To read and write data at a specific position in a file.
D) To create directories.

Answer: Option C

Explanation: RandomAccessFile is used to read and write data at a specific position in a file.

17.) What is the role of the BufferedInputStream class in Java file handling?

A) To read character data from a file.
B) To read binary data from a file efficiently.
C) To create a new file.
D) To append text to an existing file.

Answer: Option B

Explanation: BufferedInputStream is used for efficient reading of binary data from a file.

18.) Which class is used to represent a directory in Java’s file handling?

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

Answer: Option B

Explanation: The File class represents files and directories in Java’s file handling.

Leave a Reply

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