Showing 71–80 of 100 questions
in I/O Streams
What is the serialVersionUID used for in Java serialization?
A
To store the Java version number
B
To identify the class version for deserialization compatibility
C
To encrypt the serialized object
D
To track the number of times an object was serialized
Correct Answer:
B. To identify the class version for deserialization compatibility
EXPLANATION
serialVersionUID identifies class versions. Mismatched versions cause InvalidClassException during deserialization.
In a CSV file processing application, why is BufferedReader preferred over FileReader for reading large files?
A
BufferedReader automatically parses CSV format
B
BufferedReader reduces I/O operations by reading data in chunks
C
FileReader cannot read text files
D
BufferedReader is faster at character encoding
Correct Answer:
B. BufferedReader reduces I/O operations by reading data in chunks
EXPLANATION
BufferedReader buffers input, reducing system calls and I/O overhead, improving performance significantly.
A RandomAccessFile is opened with mode 'r'. Which operation will throw an exception?
A
Reading bytes from the file
B
Seeking to a specific position
C
Writing bytes to the file
D
Closing the file
Correct Answer:
C. Writing bytes to the file
EXPLANATION
Mode 'r' allows read-only access. Write operations will throw IOException.
When implementing Externalizable interface, which method is mandatory?
A
serialize()
B
writeObject() and readObject()
C
writeExternal() and readExternal()
D
write() and read()
Correct Answer:
C. writeExternal() and readExternal()
EXPLANATION
Externalizable requires implementation of writeExternal() and readExternal() for complete control over serialization.
Which statement about ObjectOutputStream.reset() is correct?
A
It resets the file pointer to the beginning
B
It clears the internal object graph cache to enable re-serialization of identical objects
C
It closes the stream and reopens it
D
It deletes the serialized file
Correct Answer:
B. It clears the internal object graph cache to enable re-serialization of identical objects
EXPLANATION
reset() clears ObjectOutputStream's internal cache of serialized objects, allowing duplicate objects to be serialized again.
A developer uses PipedInputStream and PipedOutputStream in the same thread. What will happen?
A
The program will execute successfully
B
A deadlock will occur because both read and write happen in the same thread
C
The pipe will be closed automatically
D
An IOException will be thrown immediately
Correct Answer:
B. A deadlock will occur because both read and write happen in the same thread
EXPLANATION
Pipes require separate threads for reading and writing. Using both in the same thread causes deadlock.
What will be the output of reading from a ByteArrayInputStream initialized with bytes [65, 66, 67] after calling read() three times?
A
65, 66, 67
B
ABC
C
A, B, C
D
-1
Correct Answer:
A. 65, 66, 67
EXPLANATION
read() returns integer values of bytes. [65, 66, 67] are ASCII values for 'A', 'B', 'C'. Multiple read() calls return these values sequentially.
In a multi-threaded environment, which stream class provides thread-safe read/write operations without external synchronization?
A
PrintStream
B
FileOutputStream
C
BufferedOutputStream
D
PipedInputStream
Correct Answer:
A. PrintStream
EXPLANATION
PrintStream (used by System.out) is internally synchronized for thread-safety, unlike other stream classes.
A serialized object contains a field marked as 'transient'. What happens to this field when the object is deserialized?
A
It retains its original value from before serialization
B
It is set to its default value (null for objects, 0 for primitives)
C
An exception is thrown during deserialization
D
It becomes immutable
Correct Answer:
B. It is set to its default value (null for objects, 0 for primitives)
EXPLANATION
Transient fields are excluded from serialization and are initialized to default values upon deserialization.
Which of the following correctly demonstrates chaining multiple filters in Java I/O?
A
new BufferedReader(new FileReader(file))
B
new FileReader(new BufferedReader(file))
C
new Reader(new BufferedReader(new FileReader(file)))
D
new BufferedInputStream(new FileInputStream(new File(file)))
Correct Answer:
A. new BufferedReader(new FileReader(file))
EXPLANATION
Filter streams are chained from inside-out. FileReader is wrapped with BufferedReader for better performance.