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.
Which interface must a class implement to be eligible for serialization in Java, and what is the significance of implementing it with no abstract methods?
ACloneable interface; it marks the class as eligible for deep copying
BSerializable interface; it's a marker interface with no abstract methods that indicates serialization capability
CExternalizable interface; it provides default serialization behavior
DComparable interface; it enables object comparison during serialization
Correct Answer:
B. Serializable interface; it's a marker interface with no abstract methods that indicates serialization capability
EXPLANATION
Serializable is a marker interface (contains no methods) that signals to the JVM that objects of that class can be serialized. The absence of abstract methods means implementing classes don't need to override any methods; they just need to implement the interface to indicate serialization support.
Consider a scenario where you're processing a 5GB log file and need to count specific error messages. Which I/O strategy would be most memory-efficient?
ALoad the entire file into a byte array and process it
BUse BufferedReader with readLine() in a loop to process line-by-line
CUse FileInputStream to read all bytes at once
DConvert the entire file to a String and use split() method
Correct Answer:
B. Use BufferedReader with readLine() in a loop to process line-by-line
EXPLANATION
BufferedReader with readLine() processes the file line-by-line, maintaining a constant memory footprint regardless of file size. Options A, C, and D would attempt to load the entire 5GB file into memory, causing OutOfMemoryError. This is the standard approach for large file processing.
What will happen if you attempt to serialize a class that contains a non-serializable instance variable without declaring it as transient?
AThe program will compile successfully but throw NotSerializableException at runtime
BThe compiler will generate a compile-time error
COnly the serializable fields will be written to the stream
DThe non-serializable field will be automatically converted to String
Correct Answer:
A. The program will compile successfully but throw NotSerializableException at runtime
EXPLANATION
Java serialization requires all instance variables to be serializable or marked as transient. If a non-serializable object is encountered during serialization, NotSerializableException is thrown at runtime, not compile-time. There's no automatic conversion.
In a file I/O operation, you need to write objects to a file and later retrieve them. Which approach is most appropriate?
AUse FileOutputStream with DataOutputStream for object serialization
BUse ObjectOutputStream for serialization and ObjectInputStream for deserialization
CUse PrintWriter to write object toString() values
DUse ByteArrayOutputStream to store objects in memory
Correct Answer:
B. Use ObjectOutputStream for serialization and ObjectInputStream for deserialization
EXPLANATION
ObjectOutputStream and ObjectInputStream are specifically designed for object serialization and deserialization. They handle the complete object state preservation. Other options are either inefficient or unsuitable for preserving object state.
Which of the following classes is used to read primitive data types from an input stream in Java?
ADataInputStream
BBufferedInputStream
CFileInputStream
DObjectInputStream
Correct Answer:
A. DataInputStream
EXPLANATION
DataInputStream is specifically designed to read primitive data types like int, double, boolean, etc. from an input stream. BufferedInputStream provides buffering, FileInputStream reads from files, and ObjectInputStream deserializes objects.
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.