Top Java Serialization Interview Questions & Answers
Java Serialization Interview Questions is without a doubt one of the better programming languages out there in the world. Now we have been lucky sufficient to see Java develop over time and develop into the massively popular language we all know it as at present. These improvements, which have been made in Java, led to the inclusion of some actually essential features that outline how we write programs today. A kind of feature is Serialization.
In its essence, Java Serialization Interview Questions is only a mechanism used to retailer an object into the memory. So, after we say we’re serializing an object, we imply that we’re changing the thing in question from the state, which it was right into a stream of bytes. This conversion from its native state to the byte stream makes writing this object to a file a breeze.
This file can then be transported anyplace we want, and to access the object and its features, all we have to do is de-serialize the object. De-serialization, because the name suggests, is the alternative of serialization. Right here, we convert the byte’s stream into the native state of the object to make use of the object.
Java Serialization Interview Questions permits developers to write down their code with a certain level of flexibility. The power to take the thing and use it with its native property elsewhere is essential in today’s workflow. No surprise recruiters need their potential employees to know extra about object serialization in java.
Whether or not you might have used serialization in your projects or not, you can not merely let the significance of it slide. So, to help you in your endeavor of changing into a professional java developer, we now have collected some fascinating java serialization interview questions, which you will find below.
Also read: 50 Most Asked Javascript Interview Questions & Answers [2021]
Java Serialization Interview Questions
Q1. What do you imply by Serialization within the context of Java programming language?
Ans. The definition of serialization is maybe essentially the most basic but one of the frequently asked questions within the context of Java serialization. You’ll have to reply this query is almost all of the interviews. Therefore, you will need to have a superb definition of Java serialization instilled in your Brain. So, serialization is nothing however how an object written in Java is transformed into a bytes stream.
The primary goal of that is to allow the thing to be transferred to a different machine or to save the state of the object right into a file or save the object’s state right into a database. As soon as the object is successfully serialized, then we may shortly get hold of the thing again into its former glory by merely de-serializing the object.
Q2. What’s the manner wherein we are able to serialize an object in Java? Write a program to serialize and de-serialize the object.
Ans. In an interview, if you’ll be able to augment your theoretical information with the power to write down a program, the probabilities of your choice automatically improve. Additionally it is on condition that in any interview, you will be tasked to write down a basic program (on the very least a basic program), which demonstrates how serialization and de-serialization happens. Earlier than you go and write this program your self, it’s good to keep in mind one key thing about object serialization in java.
To serialize an object, you would need to write down the object that makes use of the class java.io.Serializable interface. It’s essential to just remember to are utilizing a Marker interface for the class’s object, which you need to serialize. Which means the class in question should have no written strategies within the class. This class additionally wants to inform the Java Virtual Machine that the following object must change types and shape a stream of bytes.
The code for serialization is written below.
OutputStream fout = new FileOutputStream(“ser.txt”);
ObjectOutput oout = new ObjectOutputStream(fout);
System.out.println(“Serialization process has started, serializing employee objects…”);
oout.writeObject(object1);
The code for de-serialization is written below.
InputStream fin=new FileInputStream(“ser.txt”);
ObjectInput oin=new ObjectInputStream(fin);
System.out.println(“DeSerialization process has started, displaying employee objects…”);
Employee emp;
emp=(Employee)oin.readObject();
Q3. What’s the distinction between the interfaces for Serialization and Externalizable?
Ans. This question may mean the distinction between you getting chosen for the job or not. Suppose you handle to reply this question in a really complete method. In that case, the interviewer is sure to be impressed along with your knowledge of this subject, and the probabilities of your choice for the job will routinely skyrocket. You’ll discover all of the crucial variations within the table below:
The properties on which we’re comparing each of those methods. | SERIALIZABLE | EXTERNALIZABLE |
Methods that are present within the classes of those two different interfaces | This occurs to be a marker interface. Marker interfaces can not have any member functions. They must be empty except that they should have an instruction present in them, which tells the Java Virtual Machine that this class’s object needs to be converted right into a stream of bytes. | This isn’t a maker interface which means it has some member methods. It has method’s referred to as writeExternal() and readExternal() |
What’s their default approach of serialization? | For serializable, you will see a default approach in which you’ll be able to serialize the objects which you write. All you would want to do as a programmer can be to discover a approach in which you’ll be able to combine this interface into your program. | You’ll not discover a default approach in which you’ll be able to implement serialization. You have to to write down your own methods or override the existing ones. |
What’s the approach wherein they implement the process of serialization? | You possibly can customise the best way wherein serialization is applied on this interface. Nevertheless, you can not override the present strategies. You have to implement these strategies into your individual class to acquire the degree of freedom you desire. | On this technique, you would want to override the default methods. So if you wish to implement a customized method to serialize the thing, it is best to select this interface over the default approach of Serializable. |
What’s the degree of control which they offer within the process of serialization, | You’ll discover a tiny wiggle room if you end up utilizing this interface. You additionally want to write down the default functions into your class to get essentially the most out of this method. Nevertheless, it isn’t obligatory for you to take action, which means you’ll still be capable of serialize objects with this interface with out writing the default functions into your custom class. | This interface gives glorious control over the complete process. For that reason alone, if you’re utilizing this interface, will probably be compulsory so that you can write the 2 methods into your custom class. |
What’s the constructor used whereas utilizing de-serialization, | There isn’t any constructor which is named in the course of the strategy of serialization. | There’s a name made to the constructor when serializing the objects utilizing this interface. |
This fall. Write a program wherein you implement the custom process of serialization and de-serialization.
Ans. Right here comes the difficult part. That is the question via which you’ll be able to present all of the earlier question data via a practical use case scenario. The flexibility for you to have the ability to write these programs will clearly display your experience and assist you get the job you need.
Written under you will see the customized way of writing the writeObject() method.
private void writeObject(ObjectOutputStream os) {
System.out.println(“In, writeObject() method.”);
try catch (Exception e) {
e.printStackTrace();
}
}
Written below you will see the custom implementation of de-serliasation.
private void readObject(ObjectInputStream ois) {
System.out.println(“In, readObject() method.”);
try
id=ois.readInt();
name=(String)ois.readObject();
catch (Exception e) {
e.printStackTrace();
}
}
Also read: Python vs Java in 2021: Which One You Should Choose? [Full Comparison]
Q5. How will you implement Serialisation and de-serialization utilizing the Externalizable interface?
Ans. To implement serialization and de-serialization utilizing the externalizable interface, you will want to write down the function writeExternal() and readExternal() by yourself. You’ll discover the code for each written below.
Customizing the writeExternal() method
public void writeExternal(ObjectOutput oo) throws IOException
System.out.println(“in writeExternal()”);
oo.writeInt(id);
oo.writeObject(name);
Customizing the readExternal() method
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
System.out.println(“in readExternal()”);
this.id=in.readInt();
this.name=(String)in.readObject();
Q6. Let us say that you don’t want a specific variable to be serialized. What is going to you do to prevent the member variables which you don’t want to be serialized?
Ans. It’s a extremely conceptual question. It’s essential to have knowledge of static and non-static variables to have the ability to reply this question quickly. Suppose you desire a specific variable to not get serialized. In that case, you’ll have to make them static since any static variable’s value can’t be changed, and therefore due to this cause, they may also not get serialized.
Q7. What do you imply by serialVersionUID?
Ans. For each class which we need to be serialized, they’d be given a class number. This number, which is given to each class, which is to be serialized, is known as a serialVersionUID. This ID is crucial because, on the time of getting back the object in its native kind, the Java Virtual Machine seems out for the ID, which is related to the thing.
Then it quickly refers back to the ID of the classes which have been supposed to be serialized. When it finds the corresponding class to which this object belongs, the de-serialization course of begins.
Q8. Let us say that we forgot to say or define the serialVersionUID. What would be the affect of this motion on the program which we now have written?
Ans. This question is one other basic question. You would want a chunk of sound knowledge to have the ability to reply this question appropriately. The first thing we have to make clear is that serialVersionUID is used to do version control of the thing within the question. Let us say there isn’t an ID defined for the class, so the Java compiler wouldn’t know which class the object belongs to. On the run time or when you are serializing the thing, there is not going to be any errors as a result of there is no need per se of any ID to be defined.
Nevertheless, after we need the data stream to be transformed into the object, then the Java compiler will throw an error. The compiler is not going to know which class the object belongs to, and therefore it won’t be able to find and connect all of the member functions and the variables that are associated with this object. As a result of the compiler will probably be caught on this step, it would throw an error of serialVersionUID mismatch (java.io.InvalidClassException).
Q9. In case we can not serialize, or the method of serialization is just not accessible, is there every other method by which we would be capable of transfer the thing that we wrote over a network?
Ans. There are just a few strategies wherein we might be capable of transfer the thing that we wrote over a network. You’ll discover a few of them listed below.
- You possibly can try to convert the thing into a JSON file. It’s not that difficult to transform the object right into a JSON string, and when you might have written the JSON file, conversion of it to the code file can be not very difficult. So, you’ll be able to transfer the JSON string, which you wrote over the network.
- You may as well use the Hibernate tool (that is an ORM tool). This tool permits the object to persist within the database. Then the object which is written may also be very simply read later on.
- You may as well use the technology of XML. You possibly can try to convert the thing into an XML file, after which you’ll be able to quickly transfer that file by way of the network.