Saturday, July 14, 2012


10 Serialization in Java Interview Questions


Serialization in Java is an important concept to be aware of. I have seen candidates who talk about multi-threading when asked about Serialization in Java. 
1) What is the purpose of Serialization in Java or Under what circumstances will you use serialization?
Ans: Serialization is the process of persisting the object state in form of binary stream. The save state can be stored in a file or sent over the network.
2) What happens when an object is serialized and then a changes is made in the member variable of the class and later on the earlier serialized object is de-serialized?
Ans: Every time an object is serialized, a version number is written on the stream. This version number knows as serialVersionUID is automatically calculated by Java when no value is specified by the program. Whenever there is a change in a class, a new serialVersionUID value is calculated and the same is used for serialization/deserialization. If there is a change in serialVersionUID then an InvalidClassException is raised by JVM.
3) What happens if a class implements Serializable interface but the super class doesn’t and the sub class’s object is serialized?
Ans: If the super class has all inheritable members marked as Serializable then there will be no exception but if there is a member variable which is inherited from the super class but not marked as Serializable then a run-time exception will thrown. This rule can be remembered by using the example that Object class is not Serializable yet any class can be serialized if it is marked as Serializable.
4) How will you customize the serialization/deserialization operation?
Ans: The use of Externalizable interface can make the serialization/de-serialization operation to be customizable.
5) What happens to the private member of a class whose object is serialized or what are the security concerns with serialization? What can be done to tackle the security concerns?
Ans: The non-transient member variables are also serialized along with the serialized object and that is why the Object class is not marked as Serializable as it can cause security flaws in many applications. One way to protect private members to be read by anyone from the serialized data is to encrypt the serialized object state.
6) Can you give an example where you used or saw serialization being used?
Ans: A good example is EJB where all EJB method calls are sent over the network using Serialization.
7) Is there a way by which serialization can be made faster?
Ans: The use reflection can make serialization faster as explained in my earlier blog post.
8 ) What are marker or tag interfaces?
Ans: These are the interfaces which don’t have any methods or static variables declared. These interfaces are used to mark that a particular feature is available to a class. For e.g. Serializable and Cloneable.
9) What are transient variables?
Ans: These are the variables which don’t participate in the serialization operation. account_balance is a good example for a variable to marked as transient.
10) Which type of variables are not serialized when serializing an object?
Ans: static and transient member variables are not serialized when serializing an object. The serialVersionUID is an exception to this rule as it is a static variable and is serialized .
Related posts:

No comments: