13 Java Serialization Interview Questions with Answers for 3 to 5 Years Experienced

There is no doubt that Java is vast and there are some Java topics which many Java developers rarely explore in work but they are important from a Java interview point of view. Serialization is one of them which is rarely used in practice but quite popular during Java interviews. It's also one of the difficult topics to master and that's why I am going to share some frequently asked Java Serialization interview questions in this article.  I don't know why people ask so many questions from Serialization if not everyone uses it but I have always seen some questions from Java realization. These are also the toughest and confusing questions to answer and more likely cannot be answered by an average Java developer.

I still remember when I was asked about the readObject() and writeObject() method on one of the Java interviews at a big investment bank during the early days of my career. That was the first time I heard about those methods and I couldn't answer. 

I am sure, I wasn't alone as there were many Java developers who haven't heard about those two methods during the early stage of their career.

I have come a long way since then and learned a lot about Serialization in Java and seen them in the real world. To be honest with you, it is a very complex feature of Java and if you have to touch a code that uses Serialization, you will more likely break it than fix it.

I was given a task to add a new field in one of our domain class Order.java which implemented Serializable. I added the field only to see NotSerlizableException in our log file then I realized that any field you are going to add must implement either a Serializable or Externalizable interface if it's not transient or static.

There are many such fine details which you can only know by hard reading or real practical experience. Though, one book which helped me a lot was Effective Java. It has some of the best material converting this topic and I strongly suggest every Java developer read this book and all the items pertaining to Serialization in Java.




13 Serialization Questions with Answers from Java Interviews

Without wasting any more of your time, here are some Java questions from the Serialization topic, you need more questions to prepare well though.


1. What is a Serializable interface? What is the purpose of it? (answer)
Serializable is a marker interface and it indicates that the object of any class which implements a Serializable interface can be stored on the disc using JVM's default serialization mechanisms or transferred via the network.


2. What is the difference between the Serializable and Externalizable interface? (answer)
Both interfaces allow your object to be sterilized or converted to aq binary format which can be saved into a disc or transfers over the network but a key difference between them is that Serializable uses the default serialization mechanism of JVM and Externalizable provide hooks and callbacks to do things before and after realization. 

By the way, if you are new to Java then I also suggest you join a comprehensive online Java course like The Complete Java Masterclass by Tim Buchalka on Udemy. This 80+ hour course is one of the most comprehensive and up-to-date courses. It's also very affordable and you can buy this course for just $10 on Udemy sales. 

Java Interview Questions on Serialization and Deserialization



3. What is a transient variable? What is the purpose of it? (answer)
transient variables are not saved during the Serialization process. As the name suggests they are transient and not really part of the object's state. You can use a transient variable to exclude certain fields from being serialized like a field that is not serializable should be either marked transient or static.


4. What is SerialVersionUID in Java? Why it's important? (answer)
SerialVersionUID is an identifier generated during the Serialization process if not defined in the class. The danger of not specifying explicitly is that the default logic goes generate this id depends upon class metadata like a number of fields, which means if you add new fields or remove olds fields the id will be different.

This means you may not be able to restore the old serialized instances with the new version of your code. When you define SerialVersionUID as public static final long constant then the default serialization mechanism doesn't generate it, hence it becomes independent of class metadata.

 As I have said, a good knowledge of this topic is very important for Java programmers and if you want to learn more, I strongly suggest you join a good Java course like Java In-Depth: Become a Complete Java Engineer! on Udemy.


13 Java Serialization Interview Questions and Answers



5. How many methods we have in the Serializable interface? (answer)
None, java.io.Serializable is a marker interface, it doesn't have any method.


6. What is a marker interface in Java? (answer)
An interface without any method. Such an interface is just an indication to the compiler, JVM, or some tools for some special processing. They are similar to annotation and hence obsolete once annotation was introduced in Java 5.

7. Can you add a field in a Serializable class that doesn't implement a Serializable interface? (answer)
No, that will throw NotSerializableException, if you absolutely want to add it then mark it static or transient, but the state of that object will not be saved when the object will be serialized.

8. How do you serialize an object in Java? (answer)
In order to serialize an object make sure it implements java.io.Serializable and then use ObjectOutputStream.writeObject() method to save the object into a disk.

9. How Serialization of an object works in Java? (answer)
When you serialize an object, first its superclass is serialized, and then the subclass. If any field doesn't implement the Serializable interface (only reference type) then it will throw NotSerializableException. Though, if you want to learn more about Serialization in Java then I also suggest you go through a comprehensive Java course like The Complete Java Masterclass on Udemy.

difficult Serialization interview questions and answers




10. What about static variables? Are they Serialized? (answer)
No, static variables are not serialized. Since static is a class level variable i.e. it has the same value for each instance. This can create an issue if your serialized object id dependent upon the value of a static variable because when you deserialize the object, it will get the value for static variable what other instances will have, this may not be the same. So, don't make your serialized object depends upon the static variable.


11. What is the difference between the transient and volatile variables in Java? (answer)
They are completely different, volatile is used in multi-threading to instruct the compiler that variable will be accessed by multiple threads and provide ordering and visibility guarantee, while the transient variable is used to exclude fields from being persisted as part of serialization.

12. Can you make a subclass NotSerializable if the Superclass is Serializable? Is it mandatory for a subclass to implement the Serializable interface? (answer)
Yes, you can make the subclass NotSerializable. It's not mandatory for a subclass to be a Serializable class even if the superclass is serializable.


13. Do you know any alternative to Serialization for Java applications? (answer)
Yes, there are a couple of alternatives to Serialization in Java. For example, you can develop your own encoder and decoder to convert your Java objects to a binary format like a byte array where positions are used to mark for a particular field.

This is very popular on high-speed messaging and high-frequency training applications and many exchange protocols like Arrowhead of the Tokyo Stock Exchange and Omnet protocol of Hong Kong future exchange use this mechanism.

Another popular alternative of Serialization in Java is Google's protocol buffer which allows you to safely convert your objects to a pre-defined binary format known as protobuf. You can also use gRPC to develop server-side applications. If you want to learn more about Google Protocol Buffer then I highly recommend you go through Complete Guide to Protocol Buffers 3 in Java by Stephane Maarek on Udemy.

Google protocol buffer and difficult Serialization interview questions and answers


That's all about the Serialization interview questions for Java programmers. You can use these questions to learn more about Serialization and also fill gaps in your learning. If you haven't read then I strongly suggest you read the Effective Java book, particularly all items on Serialization which contain a lot of wisdom, best practices, and advice from Joshua Bloch about effectively and safely using Serialization in Java.


Other Interview Questions Articles you may like to explore
  • Top 30 Spring Interview Questions with Answers (questions)
  • 20+ Spring Boot Interview Questions with answers (questions)
  • Top 10 Spring Framework Interview Questions with Answers (see here)
  • 50+ Data Structure and Algorithms Problems from Interviews (questions)
  • 10 Hibernate Interview Questions for Java EE developers (see here)
  • Review these 50 Java questions before interviews (review)
  • 10 JDBC Interview Questions for Java Programmers (questions)
  • 15 Java NIO and Networking Interview Questions with Answers (see here)
  • 20  Java Design Pattern Questions asked on Interviews (see here)
  • 10 popular Struts Interview Questions for Java developers (list)
  • 130+ Java Interview Questions from the last 5 years (questions)
  • 10 frequently asked Servlet Interview Questions with Answers (see here)
  • 12  RESTful Web Services Questions from Interviews (read here)
  • 15 Data Structure and Algorithm Questions from Java Interviews (read here)
  • Top 10 Trick Java Interview Questions and Answers (see here)
  • Top 40 Core Java Phone Interview Questions with Answers (list)

Thanks for reading this article so far. If you like these Java Serialization interview questions and Answers then please share them with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are new to Java and looking for some online courses to kick-start your Java journey then you can also take a look at the Practice Java by Building Projects course on Udemy. It's a free course and you don't need to pay any amount. All you need is to create a free Udemy account to enroll in this course.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.