Serialization in Java Interview Questions And Answers for experienced professionals from Codingcompiler. These Serialization in Java interview questions were asked in various interviews conducted by top multinational companies across the globe. We hope that these interview questions on Serialization in Java will help you in cracking your job interview. All the best and happy learning.
Serialization in Java Interview Question
Serialization In Java Interview Questions and Answers
Frequently asked Serialization in Java Interview Questions
Advanced Serialization in Java Interview Questions and Answers
Serialization in Java Interview Question
- What is Serialization in Java?
- How to make a Java class Serializable?
- How many methods Serializable has? If no method then what is the purpose of the Serializable interface?
- What is the need of Serialization?
- Do we need to implement any method of Serializable interface to make an object serializable?
- Are the static variables saved as part of serialization?
- What is the purpose of Serialization?
- How to serialize an object in Java?
- Is it necessary to implement Serializable interface if you want to serialize any object?
- Can you Serialize static variables?
Serialization In Java Interview Questions and Answers
Q1. What is Serialization in Java?
Answer: Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. Java Serialization is one of important concept but it’s been rarely used as persistence solution and developer mostly overlooked Java serialization API.
Q2. How to make a Java class Serializable?
Answer: To make a Java object serializable we implement the java. io. Serializable interface. The ObjectOutputStream class contains writeObject() method for serializing an Object.
Q3. How many methods Serializable has? If no method then what is the purpose of Serializable interface?
Answer: Serializable interface exists in java.io package and forms core of java serialization mechanism. It doesn’t have any method and also called Marker Interface in Java. When your class implements java.io.Serializable interface it becomes Serializable in Java and gives compiler an indication that use Java Serialization mechanism to serialize this object.
Q4. What is the need of Serialization?
Ans) The serialization is used :-
- To send state of one or more object’s state over the network through a socket.
- To save the state of an object in a file.
- An object’s state needs to be manipulated as a stream of bytes.
Q5. Do we need to implement any method of Serializable interface to make an object serializable?
Ans) No. Serializable is a Marker Interface. It does not have any methods.
Q6. Are the static variables saved as the part of serialization?
Ans) No. The static variables belong to the class are not the part of the state of the object so they are not saved as the part of serialized object.
Q7. What is the purpose of Serialization?
Answer: Serialization is usually used when the need arises to send your data over network or stored in files. By data I mean objects and not text. Now the problem is your Network infrastructure and your Hard disk are hardware components that understand bits and bytes but not Java objects. Serialization is the translation of your Java object’s values/states to bytes to send it over network or save it.
Q8. How to serialize an object in Java?
Answer: For a class to be serialized successfully, two conditions must be met:
The class must implement the java.io.Serializable interface.
All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.
So if you want a class object to be serialized, all you need to do is implement the java.io.Serializable interface. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Java API takes care of the serialization protocol and persists the java object in a file in conformance with the protocol. De-serialization is the process of getting the object back from the file to its original form.
When serializing an object to a file, the standard convention in Java is to give the file a .ser extension.
Q9. Is it necessary to implement Serializable interface if you want to serialize any object?
Answer:
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.
Q10. 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.
Frequently asked Serialization in Java Interview Questions
Q11. How can you avoid certain member variables of class from getting Serialized?
Answer: Mark member variables as static or transient, and those member variables will no more be a part of Serialization.
Q12. Difference Between Serializable And Externalizable Interface In Java?
Answer :
Externalizable provides writeExternal() and readExternal() methods which gives flexibility to override java serialization mechanism instead of using on default serialization.
Q13. Is Serializable A Marker Interface In Java?
Answer : Yes. It has no methods.
Q14. What Is Serialversionuid In Java?
Answer :
Every time an object is serialized the Java serialization mechanism automatically computes a hash value called serialVersionUID. ObjectStreamClass’s computeSerialVersionUID() method passes the class name, sorted member names, modifiers, and interfaces to the secure hash algorithm (SHA), which returns a hash value.The serialVersionUID is also called suid.
Q15. How do you serialize an object using Serializable interface?
Answer: To serialize an object it should implement Serializable interface. In the example below, Rectangle class implements Serializable interface. Note that Serializable interface does not declare any methods to be implemented.
Below example shows how an instance of an object can be serialized. We are creating a new Rectangle object and serializing it to a file Rectangle.ser.
Q16. When will you use Serializable or Externalizable interface? and why?
Answer: Most of the times when you want to do a selective attribute serialization you can use Serializable interface with a transient modifier for variables not to be serialized. However, the use of the Externalizable interface can be really effective in cases when you have to serialize only some dynamically selected attributes of a large object.
Let’s take an example, Some times when you have a big Java object with hundreds of attributes and you want to serialize only a dozen dynamically selected attributes to keep the state of the object you should use Externalizable interface writeExternal method to selectively serialize the chosen attributes.
In case you have small objects and you know that most or all attributes are required to be serialized then you should be fine with using Serializable interface and use of transient variable as appropriate.
Q17. What would happen if the SerialVersionUID of an object is not defined?
Answer: If you don’t define serialVersionUID in your serializable class, Java compiler will make one by creating a hash code using most of your class attributes and features. When an object gets serialized, this hash code is stamped on the object which is known as the SerialVersionUID of that object. This ID is required for the version control of an object. SerialVersionUID can be specified in the class file also. In case, this ID is not specified by you, then Java compiler will regenerate a SerialVersionUID based on updated class and it will not be possible for the already serialized class to recover when a class field is added or modified. Its recommended that you always declare a serialVersionUID in your Serializable classes.
Q18. What are Transient and Volatile Modifiers
Answer:
A transient variable is a variable that may not be serialized i.e. the value of the variable can’t be written to the stream in a Serializable class. If you don’t want some field to be serialized, you can mark that field transient or static. In such a case when the class is retrieved from the ObjectStream the value of the variable is null.
Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.
Q19. Can a Serialized object be transmitted through network?
Answer: Yes, a Serialized object can be transmitted via the network as Java serialized object remains in form of bytes. Serialized objects can also be stored in Disk or database as Blo.
Q20. What is transient variable in Java?
Answer: Transient variables are not included in the serialization and are not the part of the object’s serialized state. A transient variable can be created by specifying transient keyword.
Advanced Serialization in Java Interview Questions and Answers
Q21. What are the compatible and incompatible changes in Java Serialization Mechanism?
Answer: Adding a new field or method is a compatible change and changing class hierarchy or UN-implementing Serializable interface are non-compatible changes.
Q22. How many methods Serializable has? If no method then what is the purpose of the Serializable interface?
Answer: Serializable interface exists in java.io package and forms core of java serialization mechanism. It doesn’t have any method and also called Marker Interface in Java. When your class implements java.io.Serializable interface it becomes Serializable in Java and gives compiler an indication that use Java Serialization mechanism to serialize this object.
Q23. While serializing you want some of the members not to serialize? How do you achieve it?
Answer:
Another frequently asked Serialization interview question. This is sometime also asked as what is the use of transient variable, does transient and static variable gets serialized or not etc. so if you don’t want any field to be part of object’s state then declare it either static or transient based on your need and it will not be included during Java serialization process.
Q24. Suppose super class of a new class implement Serializable interface, how can you avoid new class to being serialized?
Answer: Using the custom serialization you can provide definition of writeObject method where you can throw NotSerializableException.
Q25. Is constructor of super class called during DeSerialization process of subclass (Important)?
Answer: It is depends on whether our superclass has implemented Serializable or not.
If superclass has implemented Serializable – constructor is not called during DeSerialization process.
If superclass has not implemented Serializable – constructor is called during DeSerialization process.
Q26. what is use of serialization in java?
Answer:
Persist data for future use.
Send data to a remote computer using such client/server Java technologies as RMI or socket programming.
“flatten” an object into array of bytes in memory.
Exchange data between applets and servlets.
Store user session in Web applications.
To activate/passivate enterprise java beans.
Send objects between the servers in a cluster.
Q27. If we have a Serializable class “MyClass” which extends “MySuperClass”. What happens to the properties of “MySuperClass”, when we serialize/de-serialize “MyClass”?
Ans: If the properties inside the “MySuperClass” are serializable like primitives, serializable classes then all these will be available in the sub-class during/after serialization. However, if there are non-serializable properties, then there should be a default constructor in “MySuperClass” provided.
Q28. What code would you use in a shell script to determine if a directory exists?
Answer:
The UNIX test command with the -d option can be used to determine if a directory exists.
Details:
The following test command expression would be used to verify the existence of a specified directory, which is stored in the variable $mydir:
if [ -d $mydir ]
then
command(s)
fi
If the value stored in the variable mydir exists and is a directory file, the command(s) located between then and fi will be executed.
You can consult the test command’s man page (“$ man test”) to see what test command options are available for use.
Q29. Within a UNIX shell scripting loop construct, what is the difference between the break and continue?
Answer:
Using break within a shell scripting loop construct will cause the entire loop to terminate. A continue will cause the current iteration to terminate, but the loop will continue on the next iteration.
Q30. What is Perl unshift array function?
Ans: The Perl shift array function adds a new element at the start of an array.
Q31. What is Perl pop array function?
Ans: The Perl pop array function removes the last element of an array.
Related Interview Questions
- SAP HANA Interview Questions
- Core Java Interview Questions
- JSF Interview Questions
- JSP Interview Questions
- JPA Interview Questions
- Spring Framework Interview Questions
- Spring Boot Interview Questions
- Core Java Multiple Choice Questions
- 60 Java MCQ Questions And Answers
- Aricent Java Interview Questions
- Accenture Java Interview Questions
- Advanced Java Interview Questions For 5 8 10 Years Experienced
- Core Java Interview Questions For Experienced
- GIT Interview Questions And Answers
- Network Security Interview Questions
- CheckPoint Interview Questions
- Page Object Model Interview Questions
- Apache Pig Interview Questions
- Python Interview Questions And Answers
- Peoplesoft Integration Broker Interview Questions
- PeopleSoft Application Engine Interview Questions