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.
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 will be the output of the following code?
FileOutputStream fos = new FileOutputStream("test.txt", true);
What does the 'true' parameter indicate?
AThe file should be read-only
BThe file should be overwritten
CThe data should be appended to the existing file
DThe file should be encrypted
Correct Answer:
C. The data should be appended to the existing file
EXPLANATION
The second parameter 'true' in FileOutputStream constructor indicates append mode. If 'false' or omitted, the file is overwritten. With 'true', new data is appended to the existing file.
Which interface must be implemented to make an object serializable in Java?
ACloneable
BSerializable
CComparable
DIterable
Correct Answer:
B. Serializable
EXPLANATION
The Serializable interface must be implemented to serialize objects. It is a marker interface with no methods. Classes implementing it can be serialized using ObjectOutputStream.
Which class provides buffering capability to improve I/O performance?
AFileInputStream
BBufferedInputStream
CByteArrayInputStream
DPipedInputStream
Correct Answer:
B. BufferedInputStream
EXPLANATION
BufferedInputStream wraps another InputStream and buffers input, reducing the number of actual read operations and improving performance significantly.
Which interface must be implemented by a class to make its objects serializable?
ACloneable
BSerializable
CComparable
DIterable
Correct Answer:
B. Serializable
EXPLANATION
The Serializable interface is a marker interface that indicates a class can be serialized. It's found in java.io package and requires no methods to be implemented.