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.
In the try-with-resources statement, what happens to resources automatically?
AThey are flushed but not closed
BThey are closed in reverse order of opening
CThey are closed in the order they were opened
DThey must be manually closed in finally block
Correct Answer:
B. They are closed in reverse order of opening
EXPLANATION
Try-with-resources automatically closes resources in reverse order (LIFO). All resources must implement AutoCloseable. This ensures proper resource cleanup even if exceptions occur.
Which class wraps a byte stream to handle character encoding/decoding?
AInputStreamReader
BBufferedReader
CFileReader
DCharArrayReader
Correct Answer:
A. InputStreamReader
EXPLANATION
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.).
What happens if you try to read from a closed stream?
AIt returns -1
BIt returns null
CIt throws IOException
DIt silently fails
Correct Answer:
C. It throws IOException
EXPLANATION
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.
You need to read a file line by line efficiently. Which approach is best?
AFileInputStream with byte-by-byte reading
BBufferedReader with readLine()
CFileReader with manual character buffering
DScanner with default buffer
Correct Answer:
B. BufferedReader with readLine()
EXPLANATION
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.
What is the purpose of the serialVersionUID field in a serializable class?
ATo identify the version of the JVM
BTo ensure version compatibility during deserialization
CTo encrypt the serialized data
DTo improve serialization speed
Correct Answer:
B. To ensure version compatibility during deserialization
EXPLANATION
serialVersionUID is used to verify that sender and receiver have compatible versions of a Serializable class. If serialVersionUID doesn't match during deserialization, InvalidClassException is thrown.
Which exception is thrown when you try to serialize an object that contains non-serializable fields?
AIOException
BNotSerializableException
CSerializationException
DInvalidObjectException
Correct Answer:
B. NotSerializableException
EXPLANATION
NotSerializableException is thrown when attempting to serialize an object whose class does not implement Serializable or contains non-serializable fields.