Java Programming — I/O Streams
Java OOP, collections, multithreading
20 Questions 10 Topics Take Test
Advertisement
Showing 1–10 of 20 questions in I/O Streams
Q.1 Hard I/O Streams
In a multi-threaded application, multiple threads are writing to the same file simultaneously using FileOutputStream. What is the primary issue and best solution?
A FileOutputStream is thread-safe by default, no issues will occur
B Data corruption can occur; use synchronized blocks or PrintWriter with autoFlush enabled
C Only the last thread's data will be written; use multiple FileOutputStreams
D FileOutputStream will throw ConcurrentModificationException automatically
Correct Answer:  B. Data corruption can occur; use synchronized blocks or PrintWriter with autoFlush enabled
EXPLANATION

FileOutputStream is NOT thread-safe. When multiple threads write simultaneously, race conditions can cause data corruption or interleaved writes. Solutions include: synchronizing access using synchronized blocks, using thread-safe wrapper classes, or employing a single-threaded writer pattern with a queue. PrintWriter with autoFlush provides some buffering protection.

Take Test
Q.2 Hard I/O Streams
What is the primary advantage of PushbackInputStream?
A It reads data faster than regular InputStream
B It allows pushing back bytes to be read again
C It automatically compresses data
D It provides thread-safe reading
Correct Answer:  B. It allows pushing back bytes to be read again
EXPLANATION

PushbackInputStream allows you to 'unread' bytes using the unread() method. This is useful for parsers that need to look ahead without consuming the data.

Take Test
Q.3 Hard I/O Streams
Consider a scenario where you need to read a large binary file efficiently without loading it entirely into memory. Which approach combines best practices?
A FileInputStream with small fixed-size buffer array
B BufferedInputStream with DataInputStream for typed data
C FileInputStream with manual buffering
D ByteArrayInputStream after reading entire file
Correct Answer:  B. BufferedInputStream with DataInputStream for typed data
EXPLANATION

BufferedInputStream provides automatic buffering (8KB default), and DataInputStream allows reading typed data. Together they enable efficient reading of large binary files without loading everything into memory.

Take Test
Q.4 Hard I/O Streams
When deserializing an object, which methods are called in order?
A Constructor, then readObject()
B readObject() only, constructor is not called
C readObject(), then constructor
D Depends on the class implementation
Correct Answer:  B. readObject() only, constructor is not called
EXPLANATION

During deserialization, the constructor is NOT called. readObject() restores the object state directly from the serialized data. This is why transient fields need special handling.

Take Test
Q.5 Hard I/O Streams
What is the main advantage of using RandomAccessFile over sequential streams?
A Faster reading speed
B Ability to seek to any position in the file
C Smaller memory footprint
D Better thread safety
Correct Answer:  B. Ability to seek to any position in the file
EXPLANATION

RandomAccessFile allows you to read/write at any position in a file using seek() method. Sequential streams can only read/write sequentially from current position.

Take Test
Advertisement
Q.6 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.7 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.8 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.9 Hard I/O Streams
For developing a high-performance file copying utility handling files up to 10GB, which approach is optimal?
A Use FileInputStream/FileOutputStream with 1KB buffer
B Use BufferedInputStream/BufferedOutputStream with large buffer size
C Use FileChannel.transferTo() or NIO channels with direct buffers
D Use ObjectInputStream for binary data
Correct Answer:  C. Use FileChannel.transferTo() or NIO channels with direct buffers
EXPLANATION

FileChannel.transferTo() uses OS-level optimizations for file copying, and NIO channels with direct buffers are most efficient for large file transfers.

Take Test
Q.10 Hard I/O Streams
Which serialization approach maintains backward compatibility better?
A Updating serialVersionUID each time the class changes
B Never using serialVersionUID
C Keeping serialVersionUID constant and managing version compatibility in readObject()
D Using random serialVersionUID values
Correct Answer:  C. Keeping serialVersionUID constant and managing version compatibility in readObject()
EXPLANATION

Maintaining a constant serialVersionUID and implementing version-aware readObject() methods allows deserializing objects from different class versions.

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