In a scenario where you need to write formatted output (like printf), which class should be used?
Answer: C
PrintWriter provides print() and printf() methods for formatted output. It is the best choice for formatted writing to files or streams.
Q.42Medium
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.43Medium
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.44Medium
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.45Medium
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.
Advertisement
Q.46Medium
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.).
Q.47Medium
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.48Medium
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.49Medium
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.50Medium
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.