What is the difference between read() and read(byte[] b) methods in InputStream?
Answer: A
read() reads and returns a single byte as an int (0-255 or -1 for EOF). read(byte[] b) reads bytes into an array and returns the number of bytes read.
Q.82Medium
Which exception is thrown when you try to serialize an object that contains non-serializable fields?
Answer: B
NotSerializableException is thrown when attempting to serialize an object whose class does not implement Serializable or contains non-serializable fields.
Q.83Medium
You need to read a file line by line efficiently. Which approach is best?
Answer: B
BufferedReader with readLine() is the most efficient and convenient way to read files line by line. It provides buffering and a dedicated method for reading lines.
Q.84Medium
What happens if you try to read from a closed stream?
Answer: C
Attempting to read from a closed stream throws IOException. The stream should be checked and reopened if needed, or proper resource management (try-with-resources) should be used.
Q.85Medium
Which class wraps a byte stream to handle character encoding/decoding?
Answer: A
InputStreamReader is a bridge between byte streams and character streams. It converts bytes to characters using specified character encoding (UTF-8, ISO-8859-1, etc.).
Advertisement
Q.86Medium
In the try-with-resources statement, what happens to resources automatically?
Answer: B
Try-with-resources automatically closes resources in reverse order (LIFO). All resources must implement AutoCloseable. This ensures proper resource cleanup even if exceptions occur.
Q.87Hard
What is the main advantage of using RandomAccessFile over sequential streams?
Answer: B
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.
Q.88Hard
When deserializing an object, which methods are called in order?
Answer: B
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.
Q.89Hard
Consider a scenario where you need to read a large binary file efficiently without loading it entirely into memory. Which approach combines best practices?
Answer: B
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.
Q.90Hard
What is the primary advantage of PushbackInputStream?
Answer: B
PushbackInputStream allows you to 'unread' bytes using the unread() method. This is useful for parsers that need to look ahead without consuming the data.
Q.91Easy
Which of the following classes is used to read primitive data types from an input stream in Java?
Answer: A
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.
Q.92Medium
In a file I/O operation, you need to write objects to a file and later retrieve them. Which approach is most appropriate?
Answer: B
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.
Q.93Medium
What will happen if you attempt to serialize a class that contains a non-serializable instance variable without declaring it as transient?
Answer: A
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.
Q.94Medium
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?
Answer: B
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.
Q.95Easy
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?
Answer: B
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.
Q.96Hard
In a multi-threaded application, multiple threads are writing to the same file simultaneously using FileOutputStream. What is the primary issue and best solution?
Answer: B
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.