Java MCQs – Input/Output Operations

11.) In Java, which class is used to write binary data to an output stream?

A) DataInputStream
B) FileInputStream
C) ObjectOutputStream
D) FileOutputStream

Answer: Option D

Explanation: The FileOutputStream class is used to write binary data to an output stream in Java.

12.) Which method is used to check if there are more characters to read from an input stream in Java?

A) hasNext()
B) hasMore()
C) hasNextLine()
D) hasMoreChars()

Answer: Option C

Explanation: The hasNextLine() method is used to check if there are more lines to read from an input stream.

13.) Which class is used for formatted output in Java?

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

Answer: Option B

Explanation: The PrintWriter class is used for formatted output in Java.

14.) What is the purpose of the “append” parameter in FileWriter’s constructor?

A) To specify the file mode (read or write)
B) To specify the file’s encoding
C) To indicate whether to create a new file or append to an existing one
D) To enable or disable character encoding

Answer: Option C

Explanation: The “append” parameter in FileWriter’s constructor indicates whether to create a new file or append to an existing one.

15.) Which class is used to write formatted binary data to an output stream in Java?

A) PrintWriter
B) DataOutputStream
C) OutputStreamWriter
D) BinaryWriter

Answer: Option B

Explanation: The DataOutputStream class is used to write formatted binary data to an output stream in Java.

16.) What is the purpose of the “BufferedWriter” class in Java?

A) To read binary data from an input stream
B) To write binary data to an output stream
C) To write character-based data efficiently to an output stream
D) To read character-based data efficiently from an input stream

Answer: Option C

Explanation: The BufferedWriter class is used to write character-based data efficiently to an output stream in Java.

17.) Which method is used to flush the output buffer in Java?

A) flush()
B) clear()
C) reset()
D) close()

Answer: Option A

Explanation: The flush() method is used to flush the output buffer, ensuring that any buffered data is written to the underlying output stream.

18.) In Java, what is the purpose of the “DataInputStream” class?

A) To read character-based data from an input stream
B) To read primitive data types from an input stream
C) To write primitive data types to an output stream
D) To read and write binary data simultaneously

Answer: Option B

Explanation: The DataInputStream class is used to read primitive data types from an input stream in Java.

19.) Which class is used for reading text from the standard input (keyboard) in Java?

A) Console
B) System
C) Scanner
D) KeyboardReader

Answer: Option C

Explanation: The Scanner class is commonly used for reading text from the standard input (keyboard) in Java.

20.) In Java, which exception is thrown when there is an end-of-file condition while reading data from an input stream?

A) EOFException
B) FileEndException
C) EndOfFileException
D) NoEndException

Answer: Option A

Explanation: The EOFException is thrown when there is an end-of-file condition while reading data from an input stream.

Leave a Reply

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