Java Programming — I/O Streams
Java OOP, collections, multithreading
100 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 100 questions in I/O Streams
Q.21 Medium I/O Streams
Which class is used to read primitive data types and strings from a binary stream?
A BufferedInputStream
B DataInputStream
C ObjectInputStream
D SequenceInputStream
Correct Answer:  B. DataInputStream
EXPLANATION

DataInputStream provides methods like readInt(), readDouble(), readUTF() to read primitive data types and strings from binary streams.

Take Test
Q.22 Easy I/O Streams
In Java, what is the primary purpose of the flush() method in output streams?
A To close the stream permanently
B To clear the internal buffer and write pending data
C To reset the stream position to the beginning
D To increase the buffer size
Correct Answer:  B. To clear the internal buffer and write pending data
EXPLANATION

The flush() method forces any buffered output bytes to be written out. It does not close the stream, allowing further writes after flushing.

Take Test
Q.23 Easy I/O Streams
Which interface must be implemented to make an object serializable in Java?
A Cloneable
B Serializable
C Comparable
D Iterable
Correct Answer:  B. Serializable
EXPLANATION

The Serializable interface must be implemented to serialize objects. It is a marker interface with no methods. Classes implementing it can be serialized using ObjectOutputStream.

Take Test
Q.24 Medium I/O Streams
What is the default buffer size used by BufferedInputStream?
A 512 bytes
B 1024 bytes
C 8192 bytes
D 4096 bytes
Correct Answer:  C. 8192 bytes
EXPLANATION

The default buffer size for BufferedInputStream is 8192 bytes (8 KB). This can be overridden by using the constructor BufferedInputStream(InputStream, int size).

Take Test
Q.25 Easy I/O Streams
Which of the following is NOT a character stream class in Java?
A FileReader
B BufferedWriter
C FileInputStream
D PrintWriter
Correct Answer:  C. FileInputStream
EXPLANATION

FileInputStream is a byte stream class, not a character stream. FileReader, BufferedWriter, and PrintWriter are all character stream classes.

Take Test
Advertisement
Q.26 Hard I/O Streams
In a log aggregation system, what is the primary advantage of using RandomAccessFile over sequential streams?
A It's always faster
B It allows seeking to specific positions without reading all data sequentially
C It supports threading automatically
D It automatically compresses data
Correct Answer:  B. It allows seeking to specific positions without reading all data sequentially
EXPLANATION

RandomAccessFile supports seek() operation to jump to specific file positions, useful for reading log tail or accessing specific records directly.

Take Test
Q.27 Hard I/O Streams
Which approach would you use to read and write objects to a file efficiently in a production application?
A Manually serialize each field with DataOutputStream
B Use ObjectInputStream/ObjectOutputStream with proper version control
C Convert objects to JSON and use FileWriter
D Create custom byte arrays for each object
Correct Answer:  B. Use ObjectInputStream/ObjectOutputStream with proper version control
EXPLANATION

ObjectInputStream/ObjectOutputStream with serialVersionUID ensures compatibility, versioning, and is the standard approach for object persistence in Java.

Take Test
Q.28 Medium I/O Streams
What happens when you create a FileOutputStream with an existing file?
A Throws FileAlreadyExistsException
B Appends to the existing file
C Overwrites the existing file contents
D Merges with existing content
Correct Answer:  C. Overwrites the existing file contents
EXPLANATION

FileOutputStream(String filename) by default overwrites existing file. Use FileOutputStream(File, boolean append) with true to append.

Take Test
Q.29 Hard I/O Streams
Given the code: PrintWriter pw = new PrintWriter(new FileWriter("file.txt")); What issue might occur?
A FileWriter is not suitable for text files
B Data might not be written to disk if flush() is not called before closing
C PrintWriter cannot wrap FileWriter
D Encoding issues will definitely occur
Correct Answer:  B. Data might not be written to disk if flush() is not called before closing
EXPLANATION

PrintWriter uses buffering by default. If flush() or close() is not called, buffered data may not be written to disk.

Take Test
Q.30 Medium I/O Streams
What is the correct way to ensure resources are properly closed in Java 7+?
A Call close() in finally block
B Use try-with-resources statement
C Rely on garbage collector
D Call flush() before closing
Correct Answer:  B. Use try-with-resources statement
EXPLANATION

Try-with-resources (try-with-resources statement) automatically closes AutoCloseable resources, ensuring proper cleanup even if exceptions occur.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips