In a multi-threaded application, multiple threads are writing to the same file simultaneously using FileOutputStream. What is the primary issue and best solution?
AFileOutputStream is thread-safe by default, no issues will occur
BData corruption can occur; use synchronized blocks or PrintWriter with autoFlush enabled
COnly the last thread's data will be written; use multiple FileOutputStreams
DFileOutputStream 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.
What is the primary advantage of PushbackInputStream?
AIt reads data faster than regular InputStream
BIt allows pushing back bytes to be read again
CIt automatically compresses data
DIt 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.
Consider a scenario where you need to read a large binary file efficiently without loading it entirely into memory. Which approach combines best practices?
AFileInputStream with small fixed-size buffer array
BBufferedInputStream with DataInputStream for typed data
CFileInputStream with manual buffering
DByteArrayInputStream 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.
When deserializing an object, which methods are called in order?
AConstructor, then readObject()
BreadObject() only, constructor is not called
CreadObject(), then constructor
DDepends 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.
What is the main advantage of using RandomAccessFile over sequential streams?
AFaster reading speed
BAbility to seek to any position in the file
CSmaller memory footprint
DBetter 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.
Which approach would you use to read and write objects to a file efficiently in a production application?
AManually serialize each field with DataOutputStream
BUse ObjectInputStream/ObjectOutputStream with proper version control
CConvert objects to JSON and use FileWriter
DCreate 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.
For developing a high-performance file copying utility handling files up to 10GB, which approach is optimal?
AUse FileInputStream/FileOutputStream with 1KB buffer
BUse BufferedInputStream/BufferedOutputStream with large buffer size
CUse FileChannel.transferTo() or NIO channels with direct buffers
DUse 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.
Which serialization approach maintains backward compatibility better?
AUpdating serialVersionUID each time the class changes
BNever using serialVersionUID
CKeeping serialVersionUID constant and managing version compatibility in readObject()
DUsing 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.