Java MCQs – Input/Output Operations

Java’s Input/Output operations are fundamental for handling data streams within a program. These operations enable communication between the program and external sources like files, user input, or network connections. Mastery of I/O operations is crucial for Java developers, as it underpins many real-world applications.

This collection of multiple-choice questions (MCQs) on Java I/O operations provides a practical way to reinforce knowledge and understanding of file handling, reading and writing data, and working with streams in Java. Whether for interviews or practice, these MCQs serve as a valuable resource for honing Java I/O skills and enhancing proficiency in one of the language’s key domains.

1.) Which class is used to read character-based input in Java?

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

Answer: Option B

Explanation: The BufferedReader class is used to read character-based input efficiently in Java.

2.) What is the purpose of the System.out.println() method in Java?

A) It reads input from the user.
B) It writes output to the console.
C) It opens a file for reading.
D) It closes a file.

Answer: Option B

Explanation: The System.out.println() method is used to write output to the console in Java.

3.) Which class is used for reading primitive data types from an input stream in Java?

A) PrintWriter
B) Scanner
C) InputStreamReader
D) DataInputStream

Answer: Option D

Explanation: The DataInputStream class is used for reading primitive data types from an input stream.

4.) In Java, which exception is typically thrown when there is an error during file input/output operations?

A) IOException
B) FileNotFoundException
C) NullPointerException
D) InputOutputException

Answer: Option A

Explanation: The IOException is typically thrown when there is an error during file input/output operations.

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

A) close()
B) shutdown()
C) dispose()
D) finalize()

Answer: Option A

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

6.) What is the purpose of the FileWriter class in Java?

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

Answer: Option B

Explanation: The FileWriter class is used to write characters to a file in Java.

7.) Which class is used to write formatted text to a character stream in Java?

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

Answer: Option A

Explanation: The PrintWriter class is used to write formatted text to a character stream in Java.

8.) Which method is used to write an array of characters to a file in Java?

A) writeChars()
B) writeArray()
C) write()
D) writeArrayChars()

Answer: Option C

Explanation: The write() method is used to write an array of characters to a file in Java.

9.) Which method is used to read a line of text from an input stream in Java?

A) readLine()
B) read()
C) readChar()
D) readText()

Answer: Option A

Explanation: The readLine() method is used to read a line of text from an input stream.

10.) Which class is used to read binary data from an input stream in Java?

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

Answer: Option A

Explanation: The FileInputStream class is used to read binary data from an input stream.

Leave a Reply

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