Top 20 Serialization Interview Questions and Answers

Serialization is the important topic of java interview. Java developers should know the answer to serialization interview questions.  In this article, we will look at 20 most important questions and answers of serialization. We have covered questions for both beginners and experienced professionals level.

Read Also: What is serialVersionUID in Java

1. What is Serialization in java?

A very common question asked during the java serialization interview. Every Java developer should know the answer to this question.  Serialization needed to write an object into a binary format that can be transferred over the network or stored in the database. Serialization prepares a stream of bytes of an object and the byte array consists of versionUID, class of the object, and the internal state of the object.

2. Describe the De-serialization process.

This is also a common interview question during the java interview. To build a java object from a stream of bytes is called deserialization. When a stream of bytes transferred over the network so another side of the network needs to revert back to java object. This process is called deserialization.

3. Why do we need to use Serialization in java?

Serialization actually needed for transferring the byte stream over the network. Some real-time applications using java streams are listed below:

a)    Data transmission
b)    Persistence
c)    Deep cloning
d)    Cross JVM communication
e)    Stashing

4. How can we implement Serialization in java?

The most common method to implement Serialization in java is by implementing the Serializable interface. If an object implements a Serializable interface then this object can be transferred as the stream of byte over the network. Another way is to implement the Externalizable interface.

5. What is the Marker interface?

If an interface does not contain any methods then it is known as Marker Interface. The Serializable interface is a Marker interface. JVM automatically identifies whether the class is Serializable by checking the Serializable interface is implemented or not.

6. Why the Serializable interface is called the Marker interface in Java?

This is the most important question in the java serialization interview question. The serializable interface has no methods. We know that the interface which does not contain any method is called a Marker interface. That’s why the Serializable interface is Marker Interface.

7. What is serialVersionUID?

When we make a class Serializable we have to declare a final static long variable with a value named by serialVersionUID. This variable will be serialized and passed during the byte stream transfer. During deserialization, the compiler will compare this serialVersionUID with the saved one. You can find the detail about serialVersionUID here.

8. Describe the benefits of serialVersionUID.

During the deserialization process, serialVersionUID is compared with senders serialized object. If both are not the same JVM gives InvalidClassException. This ensures the security of the class during network transmission.

9. How can we restrict some variables to be serialized?

If we want to restrict a variable from serialized, we should declare this variable as a transient variable. The transient variable can not be converted to a byte of a stream.

10. Do static variables participate in serialization?

This is also a tricky interview question. Static variables or members are belonging to a class not the instance of the class. During serialization, we only consider the current state of an object. So during Serialization, static members are ignored. There is a rule of thumb that static fields are not serialized but there is an exception serialVersionUID. serialVersionUID is also saved in the serialized object. (answer)

11. What will happen if the reference variable is not serializable?

It's an easy but tricky question during interview time. If a reference variable of a serializable class does not support the Serializable interface then it will throw NotSerializableException at runtime.

12. What will happen if a class is serializable but its superclass does not?

The serialization process will continue until the inherited class is serializable. If a superclass not serializable though it will instantiate the constructor of the superclass. When a constructor chain initiated it will not stop execution until it reaches the end of the hierarchy of classes implementing the Serializable interface.

13. Can you customize the serialization process? How can you achieve this?

Yes, we can customize the serialization process. JVM invokes ObjectOutputStream.writeObject (saveThisobject) to write an object and ObjectInputStream.readObject() to read an object. We can override these methods with our own implementation. We can add pre or post-process during Serialization or Deserialization. These methods are declared to restrict inheritance. Private methods of the integrity of class remain the same that’s why JVM can call those methods during Serialization and Deserialization.

14. Suppose parent class of a new child class implement Serializable interface, how can you avoid new child class to being serialized?

This is an important question for serialization interview. It’s a tricky question. We know child classes get the properties of parent class by default. So, child classes are also serializable. To avoid new child classes being serializable we have to override the writeObject(object) or readObject() method. Also, we have to throw the NotSerializableException exception from the methods.

15. Find out the difference between Serialization and Externalizable?

For default serialization, we use the Serializable interface but with the Externalization interface, we can have more control over serialization. Serializable is a marker interface. That’s why there are no user-defined methods. But in Externalization, we can write the method inside the interface and implement it to the child classes. For this reason, we have more control over serialization and deserialization. The Externalizable interface also extends Serializable interface

Serializable like below snapshot:

public interface Serializable {

}

An externalizable interface like below snapshot:

public interface Externalizable extends java.io.Serializable {

void writeExternal(ObjectOutput out) throws IOException;

void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;

}


16. When we should use Externalizable interface in Java?

If we need to handle the custom serialization, we should use the Externalizable interface. With this implementation, we can control the serialization process during the read or write.

Methods of Externalization listed below:

void writeExternal(ObjectOutput out) throws IOException;

void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;


17. Describe the methods of serialization and deserialization process.

There are no methods for the Serializable interface. If a class implements Serializable interface JVM treat an instance of this class as a serializable object. But with the Externalize interface, we can override defined methods.

18. How to serialize an ArrayList?

ArrayList by default implements a Serializable interface but we just need to check the inside object of ArrayList. If the object does not contain serializable object it will throw NotSerializableException exception. You can find more about how to serialize ArrayList in java here.

19. In singleton design pattern serialization becomes an issue, how you will overcome this?

During serialization or deserialization process principle of singleton class breaks. So, the readResolve() method should be overridden during a singleton serializable class declaration. Then it will create the same instance every time.

20. What are the most important classes and interfaces using the Serialization?

Most commonly used classes and interfaces are listed below:

a) java.io.Serializable
b) java.io.Externalizable
c) java.io.ObjectInputStream
d) java.io.ObjectOutputStream
e) java.io.FileInputStream
f) java.io.FileOutputStream

That's all for today, please mention in comments in case you have any questions related to Serialization interview questions and answers.

Reference:
Serializable Java doc

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry