Java Serialization interview questions and answers

Serialization interview questions

Serialization is one of most important concept in java. If you are going to face core java interview, then you might be asked some questions from Serialization.

Question 1: What is Serialization?

Java provides mechanism called serialization to persists java objects in a form of ordered or sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.So if we need to serialize any object then it can be read and deserialize it using object’s type and other information so we can retrieve original object.Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.
ObjectOutputStream has many method for serializing object but commonly used method is
Similarly ObjectInputStream has

Question 2: What is need of Serialization?

Serialization is usually used when there is need to send your data over network or to store in files. By data I mean objects and not text.

Now the problem is your Network infrastructure and your Hard disk is hardware components that understand bits and bytes but not Java objects.

Serialization is the translation of Java object’s values/states to bytes to send it over network or to save it.On the other hand, Deserialization is conversion of byte code to corresponding java objects.

Question 3: Can you explain about Concept of serialVersionUID?

serialVersionUID is used to ensure that same class(That was used during Serialization) is loaded during Deserialization.serialVersionUID is used for version control of object.You can read more at serialVersionUID in java serialization

Question 4: Is it necessary to implement Serializable interface if you want to serialize any object?

Yes, it is necessary to implement Serializable interface if you want to serialize any object. Serializable is marker interface.Marker interface in Java is interfaces with no field or methods or in simple word empty interface in java is called marker interface.

Question 5: Can you Serialize static variables?

Answer :
No,you can’t.As you know static variable are at class level not at object level and you serialize a object so you can’t serialize static variables.

Question 6: How can you customize serialization process?

Answer :
You can customize Serialization process by defining writeObject and readObject method.Java serialization provides a mechanism such that if you have private methods with particular signature then they will get called during serialization and deserialization, so in this way, we can customize Serialization process.
For example:

One thing should be kept in mind that ObjectInputStream should read data in same sequence in which we have written data to ObjectOutputStream.

Question 7: How can you avoid certain member variable of class to be serialized?

You can mark that variable as either static or transient. Let’s see a simple example using transient variable.
Transient variable is the variable whose value is not serialized during serialization. You will get default value for these variable when you deserialize it.

Let’s say you have Country class and you don’t want to Serialize population attribute as it will change with time, so you can declare population attribute as transient and it won’t serialized any more.
Transient keyword example:
Create a classed called as below:
Create as below:
When you run above program, you will get below output:
Now Create a classed called as below:
When you run above program, you will get below output:
As you can see in above example, we have declared population as transient, so after deserialization, its value became 0 (Default value for long)

Question 8: What  if superclass is Serializable?  Does that mean child class is automatically Serializable?

Answer : Yes

Question 9: What if superclass is Serializable but you don’t want subclass to be Serializable?

Answer : If you don’t want subclass to serializable then you need to implement writeObject() and readObject() method and need to throw NotSerializableException from this methods.

Question 10 :What is externalizable interface?

Answer: As name suggest it is externalilizing your serialization.If you want to customize your serialization mechanism then you can use it.It uses custom written mechanism to perform marshalling and unmarshalling of objects.[Externalizable]( “Externalizable”) interface extends Serializable interface. If you implement this interface then you need to override following methods.

Question 11 : What are differences between Serializable and Externalizable in Java?

Answer :

Marker interface
It is marker interface. You don’t have to provide implementation of any method.
Externalizable is not marker interface, you have to override writeExternal and readExternal method.
Serializable interface has less control over serialization process and it is optional to override readObject and writeObject.
Externalizable interface has more control over serialization process and it is mandatory to override writeExternal and readExternal.
JVM uses reflection to perform serialization in the case of Serializable interface which is quite slow.
Programmer have to implement readExternal and writeExternal methods but it relatively results in better performance
If you implement Externalizable interface and provide implementation of readExternal and writeExternal then it supersedes readObject and writeObject methods in that class. It is due to the fact that Externalizable extends Serializable interface.
Constructor called during Deserialization
Default constructor is not called during Deserialization process.
Default constructor is called during Deserialization process.

You may also like:

Was this post helpful?

Related Posts

  • Hibernate interview questions
    16 April

    Top 40+ Hibernate interview questions and answers

    Table of Contents1. What is ORM?2. What is Hibernate?3. What are advantages of Hibernate?4. Explain architecture of Hibernate?5. What are some core interfaces of hibernate?6. Explain brief about Session interface used in hibernate?7. Are session objects thread safe?8. Explain brief about SessionFactory object used in hibernate?9. What is Configuration class in hibernate?10. Explain brief about […]

  • Java Interview questions
    22 May

    100+ Core Java Interview Questions and Answers for 2022

    Table of ContentsIntroductionJava Interview QuestionsJava Interview TipsMore Java Interview Q&ACore java interview questionsCollections interview questionsMultithreading interview questionsJava 8 interview questionsOOPs interview questionsException Handling interview questionsString interview questionsSerialization interview questionsmethod overloading and method overriding interview questionsImmutable class interview questionsJava tricky interview questions Basic interview questions for fresher Interview questions for 5 to 7 years experiencedHow HashMap […]

  • Solid principles in java
    09 November

    SOLID Principles in Java

    Table of ContentsWhat is the meaning of S.O.L.I.D?Single Responsibility Principle (SRP)Open Closed Principle Liskov Substitution Principle Interface Segregation PrincipleDependency Inversion PrincipleWrap Up In this post, we will see 5 SOLID Principles in Java. Robert C. Martin gave five objected oriented design principles, and the acronym S.O.L.I.D is used for it. Each letter of this acronym […]

  • Java interview questions for 2 years experience
    08 November

    Java interview questions for 2 years experience

    Table of Contents1.   Can we override static method in java?2.   Can you overload main method in java?3. Can we override private methods in java?4. What is the base class for all the classes?5.   Can you list down some of important method from object class?6.   Which two methods should you override while putting the custom object as […]

  • Java interview questions for 5 years experience
    08 November

    Java interview questions for 5 years experience

    Table of Contents1. Guess the output of below program.2. Guess the output of below program.3. Guess the output of below program.4. What will happen in case of below program?5. What will happen in case of below program?6. What will happen in case of below program?7. Guess the output of below program.8. Guess the output of […]

  • Java tricky interview questions
    11 September

    Java technical interview questions

    Table of Contents1 How HashMap works in java?2 Which methods you should implement for key Object in HashMap?3. Guess the output of below program.4. Guess the output of below code.5. What are differences between Comparator and Comparable?6. What is difference between Error and Exception?7. What is ClassLoader in java?8. What are types of ClassLoader in java?9. […]


  1. Hi Arpit,

    Can you please explain the last point with example in difference between Externalizable and Serializable interfaces

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.