Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Easy Level Serialization in Java Interview Questions
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 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: May 31, 2024
Easy

Serialization in Java Interview Questions

Author Sagar Mishra
1 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

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.

Easy Level Serialization in Java Interview Questions

1.What is Serialization in java?

Ans. This is a question that any Java dev should be able to answer. 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? 

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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 Java Serialization Mechanism?

Ans. 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?

Ans. 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?

Ans. 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Medium Level Serialization in Java Interview Questions

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

Ans. 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?

Ans. 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?

Ans. 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?

Ans. Some examples of the most common 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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 actually 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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?

Ans. 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

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:

You can also consider our Interview Preparation Course to give your career an edge over others.

Happy Learning!

Live masterclass