1.
How many methods does Serializable have?
Correct Answer
D. None
Explanation
The Serializable interface does not have any methods. It is a marker interface that indicates that a class can be serialized, meaning its objects can be converted into a stream of bytes and stored or transmitted. However, it does not define any methods that must be implemented by the class. Therefore, the correct answer is "None."
2.
SerialVersionUID of a class is subject to change if not defined by user
Correct Answer
A. Yes
Explanation
The serialVersionUID of a class is a unique identifier used during serialization and deserialization to ensure that the class being serialized and the class being deserialized are compatible. If the serialVersionUID is not defined by the user, it is automatically generated based on the class's structure. This means that any changes to the class's structure, such as adding or removing fields or methods, can result in a different serialVersionUID being generated. Therefore, if the user does not define the serialVersionUID, it is subject to change.
3.
While serializing you want some of the members not to serialize? How do you achieve it?
Correct Answer
C. Both of the above
Explanation
By declaring a variable as static, it means that the variable belongs to the class rather than an instance of the class. Static variables are not serialized because they are not associated with any particular object. On the other hand, by declaring a variable as transient, it means that the variable should not be serialized. Transient variables are typically used for sensitive or unnecessary data that should not be persisted during serialization. Therefore, by using both static and transient declarations, we can achieve the goal of not serializing certain members.
4.
What will happen if one of the members in the class doesn't implement Serializable interface?
Correct Answer
B. Will throw run time exception
Explanation
If one of the members in the class doesn't implement the Serializable interface, it will throw a run time exception. This is because when an object is being serialized, all of its members are also serialized. If a member is not serializable, it cannot be converted into a byte stream and an exception will be thrown at runtime.
5.
Can you customize Serialization process or can you override default Serialization process in Java?
Correct Answer
A. Yes
Explanation
Yes, you can customize the serialization process in Java by implementing the Serializable interface and providing custom readObject() and writeObject() methods in your class. This allows you to have control over how the object is serialized and deserialized. Additionally, you can override the default serialization process by implementing the Externalizable interface, which gives you even more control over the serialization process.
6.
Suppose super class of a new class implement Serializable interface, how can you avoid new class to being serialized?
Correct Answer
B. Implement writeObject() and readObject() method in your Class and need to throw NotSerializableException from those method.
Explanation
By implementing the writeObject() and readObject() methods in the new class, we can control the serialization process. By throwing a NotSerializableException from these methods, we can prevent the new class from being serialized. This exception will be thrown whenever an attempt is made to serialize or deserialize an object of the new class, effectively avoiding serialization.
7.
Can we transfer a Serialized object via network
Correct Answer
A. Yes
Explanation
Serialized objects can be transferred via a network. Serialization is the process of converting an object into a byte stream, which can be easily transmitted over a network. The byte stream can then be deserialized at the receiving end to recreate the object. This allows for the transfer of complex data structures and objects between different systems or applications.
8.
Keeping long variable names can be costly from performance perspective while serializing and de-serializing objects
Correct Answer
A. Yes
Explanation
Long variable names can be costly from a performance perspective while serializing and de-serializing objects because longer names require more memory to store and more time to process. When objects are serialized, their variable names are also stored along with their values. Longer variable names mean more data needs to be written to disk or transmitted over a network, resulting in slower serialization and de-serialization processes. Therefore, it is advisable to use shorter and more concise variable names to optimize performance in these scenarios.
9.
Can excessive use of reflection incur performance cost?
Correct Answer
A. Yes
Explanation
Excessive use of reflection can indeed incur a performance cost. Reflection involves dynamically accessing and manipulating objects and their properties at runtime, which requires additional processing and can be slower compared to direct access. This can result in decreased performance and slower execution times, especially in performance-sensitive applications. Therefore, it is important to use reflection judiciously and only when necessary to avoid unnecessary performance overhead.
10.
True or False: To serialize an array or a collection all the members of it must be serializable
Correct Answer
A. True
Explanation
To serialize an array or a collection, all the members of it must be serializable. Serialization is the process of converting an object into a byte stream so that it can be stored in memory, transferred over a network, or saved to a file. If any member of the array or collection is not serializable, it cannot be converted into a byte stream and therefore cannot be serialized. Therefore, the statement "To serialize an array or a collection all the members of it must be serializable" is true.