Table of contents
1.
Introduction
2.
Serialization in Java Interview Questions for Fresher
2.1.
1. What is Serialization in Java?
2.2.
2. What is a serialVersionUID, and why should I use it? 
2.3.
3. What is the need for Serialization?
2.4.
4. Why are static member variables not serialized?
2.5.
5. What is a transient variable? What is the purpose of it?
2.6.
6. How can we implement Serialization in Java?
2.7.
7. What are the compatible changes and incompatible changes in the Java Serialization Mechanism?
2.8.
8. Is it necessary to implement a Serializable interface if you want to serialize any object?
2.9.
9. What is a transient variable, and what will be its value after serialization?
2.10.
10. What Is Serialversionuid In Java?
3.
Medium-Level Serialization in Java Interview Questions
3.1.
11. Which Method Is Used During Serialization And Deserialization Process In Java?
3.2.
12. What happens to the object references included in the object?
3.3.
13. What happens to the static fields of a class during serialization?
3.4.
14. What are the classes and interfaces for serialization and de-serialization that are most commonly used?
3.5.
15. What will be the value of transient variables after de-serialization?
3.6.
16. Which modifier stops variables from being serialized?
3.7.
17. How can we customize the serialization process?
3.8.
18. How can a child class avoid being serialized when a parent class already implements the Serializable interface?
3.9.
19. How to improve Java serialization performance?
3.10.
20. Let's say that we forgot to mention or define the serialVersionUID. What will impact this action on the program we have written?
4.
Hard Level Section Serialization in Java Interview Questions
4.1.
21. How many methods are in the Serializable interface? Which methods of Serializable interface should I implement?
4.2.
22. How can we avoid the serialization of a subclass?
4.3.
23. What is a marker interface in Java?
4.4.
24. How Serialization of an object works in Java?
4.5.
25. Is the constructor of a class called during the DeSerialization process?
4.6.
26. What is an externalizable interface?
4.7.
27. Do you know any alternative to Serialization for Java applications?
4.8.
28. What are compatible and incompatible changes in the Serialization process?
4.9.
29. What is the value of transient variables?
4.10.
30. Suppose the parent class of a new child class implements a Serializable interface. How can you avoid the new child class being serialized?
5.
Conclusion
Last Updated: Dec 9, 2024
Easy

Serialization in Java Interview Questions

Author Sagar Mishra
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

There is no doubt about the fact that Java is a vast language. There are some Java topics that many Java devs rarely explore in the course of their work. But they are crucial from the view of a Java interview.

Serialization in Java Interview Questions

Today we will discuss Serialization in Java Interview Questions. This is one of the most asked topics in the interview.

Serialization in Java Interview Questions for Fresher

1. What is Serialization in Java?

Serialization in Java is the process of converting an object into a byte stream for saving to a file or transmitting over a network. It allows objects to be restored later through deserialization. We need serialization to write an item into a binary format to send over the network or keep in a database. The process has the creation of a stream of bytes of an object. It holds the object's versionUID, class, and internal data.

2. What is a serialVersionUID, and why should I use it? 

It is a version number that is assigned to each serializable class. A serializable class creates a field called serialVersionUID. To declare its own serialVersionUID.The field must be :

  1. Static.
  2. Final.
  3. Long data type.

The main goal of serialVersionUID is to ensure that an object's sender and receiver have loaded classes for it. These classes are compatible with serialization. If this is not the case, then an InvalidClassException exception will occur.

3. What is the need for Serialization?

It is used when we need to send data need over a network or store it in files. By data, we mean objects rather than text. The issue now is that your network framework and hard disc are hardware components and accept bits and bytes but not Java objects.

4. Why are static member variables not serialized?

Static variables are defined as variables that belong to a class rather than an object. They retain their value even when they are out of their scope. Now, serialization is converting and storing an object's state because static variables belong to the class rather than the object. Thus they are not serialized.

5. What is a transient variable? What is the purpose of it?

Transient variables are not saved during the Serialization process. As the name implies, they do not constitute part of the object's state. We can use this variable to prevent certain fields from being serialized. For instance, a field that is not serializable should be marked transient or static.

6. How can we implement Serialization in Java?

The most common way of implementation is the Serialization Interfaces in Java. If an object implements this interface, it can be transferred as a byte stream over the network. Another option is to use the Externalizable interface.

7. What are the compatible changes and incompatible changes in the Java Serialization Mechanism?

The tough part arises while changing the class structure by adding or removing any field or method from an already serialized object. Based on the Java Serialization specification, adding any field or method is a compatible change, while changing class hierarchy is a non-compatible change.

8. Is it necessary to implement a Serializable interface if you want to serialize any object?

This is an important question from an interview point of view. Let us discuss this in our Serialization in Java Interview Questions.
Yes, if you want to serialize any object, you must implement the Serializable interface. Serializable is a marker interface. A marker interface in Java is an interface with no fields or methods or, in other words, an empty interface in Java.

9. What is a transient variable, and what will be its value after serialization?

Ans. This is also one of the important questions from an interview point of view. Let us discuss this in our Serialization in Java Interview Questions.
A transient keyword is used to make sure that the current state of certain variables in an object is not serialized. Transient is a variable modifier that disregards the variable's current and default values at the serialization time.

10. What Is Serialversionuid In Java?

When an object is serialized, the Java serialization tool generates a hash value called serialVersionUID. The computeSerialVersionUID() method of ObjectStreamClass sends the sorted member names, class names, modifiers, and interfaces to the secure hash algorithm (SHA). This returns a hash value. The serialVersionUID is also known as suid.

Medium-Level Serialization in Java Interview Questions

11. Which Method Is Used During Serialization And Deserialization Process In Java?

Java Serialization is handled by the java.io.ObjectOutputStream class. It is a filter stream wrapped around a lower-level byte stream. To serialise an object, use the ObjectOutputStream.writeObject(saveThisobject) method, and to deserialize it, use the ObjectInputStream.readObject() method.

12. What happens to the object references included in the object?

For serialization, the serialization method generates an object graph. As a result, it determines whether or not the included object references are serializable. This is a cyclical process. As a result, when an object is serialized, all included objects are serialized alongside the original object.

13. What happens to the static fields of a class during serialization?

There are three exceptions where serialization does not necessarily read and write to the stream. These are

  1. Base class fields are only handled if the base class itself is serializable.
  2. Serialization ignores static fields as they are not part of any particular state.
  3. Transient fields.

14. What are the classes and interfaces for serialization and de-serialization that are most commonly used?

Some examples of the most commonly used  serialization and de-serialization are:

  1. java.io.Externalizable.
  2. java.io.Serializable.
  3. java.io.ObjectOutputStream.
  4. java.io.ObjectInputStream.
  5. java.io.FileOutputStream.
  6. java.io.FileInputStream.
  7. java.io.FileInputStream.

15. What will be the value of transient variables after de-serialization?

This is an important question from an interview point of view. Let us discuss this in our Serialization in Java Interview Questions.
Transitory variables allow us to stop serializing the necessary values. But when we deserialize them, the default values of the transient variables take their place, and we lose the original values. So we will not create transitory variables as we could just skip generating the variable itself.

16. Which modifier stops variables from being serialized?

A modifier that stops variables from being serialized is:

  1. The transient modifier stops variables from being serialized.
  2. Like, as 0 for int data type, null for String, false for Boolean data type, etc.
  3. During de-serialization, it returns with a default value.

17. How can we customize the serialization process?

We can complete the Serialization process using the below steps:

  1. Instead of implementing the Serializable interface, in which the Marker interface has no function, implement the "java.io.Externalizable" interface. It has two methods. This will let you tailor the serialization process.These methods are writeExternal();readExternal();
  2. To serialize, use the writeExternal(); method and write custom logic.
  3. Similarly, to de-serialize use readExternal(); method and code custom logic.
     

18. How can a child class avoid being serialized when a parent class already implements the Serializable interface?

This is a vital question from an interview point of view. Let us discuss this in our Serialization in Java Interview Questions.
Even though the child class doesn't implement the Serializable interface, it becomes serializable when the parent class does.
Implement the writeObject() and readObject() methods in the child class as a workaround. Then use those methods to throw NotSerializableException.

19. How to improve Java serialization performance?

The number and size of attributes in the Java object directly affect how well the Java object is serialized. We can improve the performance by:

  1. Mark the unwanted or non-serializable attributes as transient.
  2. Serialize attributes only with NON-default values.
  3. Save only the state of the object, not the derived attributes. Sometimes, serializing them can be costly.
  4. To dynamically identify the attributes that need to be serialized, use an Externalizable interface. And implement the readExternal and writeExternal methods.

20. Let's say that we forgot to mention or define the serialVersionUID. What will impact this action on the program we have written?

The object in question is version-controlled using serialVersionUID. This is the first thing we need to make clear. Let us assume that the class has no defined ID. In this case, the Java compiler would not be able to decide the class to which the object belongs. There won't be any issues at run time or when serializing the object as an ID doesn't need to be defined.

Hard Level Section Serialization in Java Interview Questions

21. How many methods are in the Serializable interface? Which methods of Serializable interface should I implement?

In the Serializable interface, there is no method. It is an empty interface that has no attribute. The Serializable interface serves as a marker to indicate to object tools that the class is serializable. As a result, we don't use any strategies.

22. How can we avoid the serialization of a subclass?

This is a vital question from an interview point of view. Let us discuss this in our Serialization in Java Interview Questions.
A subclass of a class is also serializable if its superclass implements the Serializable interface. Implementing writeObject() and readObject() methods in the class and raising a NotSerializableException from the methods is one technique to prevent the subclass from being serialized.

23. What is a marker interface in Java?

Marker interfaces are interfaces that do not have any methods. Such an interface merely serves as a request for special processing from the compiler, JVM, or other tools. Since annotation was introduced in Java 5, they are similar to annotation and have become obsolete.

24. How Serialization of an object works in Java?

When you serialize an object, first the superclass and then the subclass are serialized. Any field that only supports reference types and doesn't implement the Serializable interface will throw the NotSerializableException exception.

25. Is the constructor of a class called during the DeSerialization process?

This question will test how well you grasp the principles of function chaining and serialization. Whether or not our object has executed Serializable or Externalizable defines the outcome.
Constructor is not called during the DeSerialization process if Serializable has been implemented.
Yet, if Externalizable has been implemented, DeSerialization will call function if it exists.

26. What is an externalizable interface?

As the name says, it externalizes your serialization. You are free to use it to modify your serialization method. A newly built method is used to do the marshaling and unmarshalling of "objects.interface," which extends the Serializable interface. The methods must be overridden if you implement this interface.

27. Do you know any alternative to Serialization for Java applications?

In Java, there are a few options for serialization. You can develop your own encoder and decoder. It will help you to convert your Java objects to a binary format like a byte array, where positions are used to mark a particular field. 

28. What are compatible and incompatible changes in the Serialization process?

Compatible changes:

  • Adding new fields.
  • Adding writeObject() / readObject() method.
  • Changing the access modifier of a field.

Incompatible changes:

  • Deletion of fields.
  • Changing a non-static field into static.
  • Modifying the writeObject() / readObject() method.

29. What is the value of transient variables?

Transitory variables cannot be serialized, so we must mark all rarely used variables as transient. Transitory variables can be initialized during deSerialization by changing the deSerialization process.

30. Suppose the parent class of a new child class implements a Serializable interface. How can you avoid the new child class being serialized?

This is a key interview topic for serialization. It's a complex question. We are aware that child classes always inherit the parent class's properties. So, child classes can also be serialized. We must override the writeObject(object) or readObject() method to prevent new child classes from being serializable. Also, each method must throw the NotSerializableException exception.

Conclusion

Mastering Java serialization is essential for developers, particularly for interview preparation. We have explored various questions related to serialization, including its concepts, methods, and performance optimization techniques. A strong understanding of serialization, including interfaces like Serializable and Externalizable, will help candidates demonstrate their expertise and readiness for Java development roles in interviews. We have discussed the topic of Serialization in Java Interview Questions. We have seen different types of Questions that are asked in the Interview exam.

We hope this blog has helped you enhance your knowledge of Serialization in Java Interview Questions.

If you want to learn more, check out our articles on:

Live masterclass