BIG
DATA

JAVA

Java Serialization Interview Questions and Answers Part-2

Read more about »
  • Java 9 features
  • Read about Hadoop
  • Read about Storm
  • Read about Storm
 
B

Basic Level Interview Questions

I

Intermediate Level Interview Questions

A

Advanced Level Interview Questions


A Can you Customize Serialization process or can you override default Serialization process in Java?

Yes you can. We all know that for serializing an object ObjectOutputStream.writeObject (saveThisobject) is invoked and for reading object ObjectInputStream.readObject() is invoked but there is one more thing which Java Virtual Machine provides you is to define these two method in your class. If you define these two methods in your class then JVM will invoke these two methods instead of applying default serialization mechanism. You can customize behavior of object serialization and deserialization here by doing any kind of pre or post processing task. Important point to note is making these methods private to avoid being inherited, overridden or overloaded. Since only Java Virtual Machine can call private method integrity of your class will remain and Java Serialization will work as normal.

A How do you perform any specific validation without affecting default java serialization mechanism?

You can use defaultReadObject() and defaultWriteObject() inside readObject() and writeObject() methods – to enable default serialization and deserialization. And then you can plugin your custom validation or business rules inside read/write methods. This way your validation methods will be automatically called by JVM, immediately after default serialization and deserialization process happens.

A When will you use Serializable or Externalizable interface? and why?

Externalizable interface can be really effective in cases when you have to serialize only some dynamically selected attributes of a large object. For 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.

A What are the ways to speed up Object Serialization? How to improve Serialization performance?

The default Java Serialization mechanism is really useful, however it can have a really bad performance based on your application and business requirements. The serialization process performance heavily depends on the number and size of attributes you are going to serialize for an object. Below are some tips you can use for speeding up the marshaling and un-marshaling of objects during Java serialization process.

  • Mark the unwanted or non Serializable attributes as transient. This is a straight forward benefit since your attributes for serialization are clearly marked and can be easily achieved using Serialzable interface itself.
  • Save only the state of the object, not the derived attributes. Some times we keep the derived attributes as part of the object however serializing them can be costly. Therefore consider calcualting them during de-serialization process.
  • Serialize attributes only with NON-default values. For examples, serializing a int variable with value zero is just going to take extra space however, choosing not to serialize it would save you a lot of performance. This approach can avoid some types of attributes taking unwanted space. This will require use of Externalizable interface since attribute serialization is determined at runtime based on the value of each attribute.
  • Use Externalizable interface and implement the readExternal and writeExternal methods to dynamically identify the attributes to be serialized. Some times there can be a custom logic used for serialization of various attributes.

A Why static variables are not serialized in Java?

The Java variables declared as static are not considered part of the state of an object since they are shared by all instances of that class. Saving static variables with each serialized object would have following problems

  • It will make redundant copy of same variable in multiple objects which makes it in-efficient.
  • The static variable can be modified by any object and a serialized copy would be stale or not in sync with current value.

A How to Serialize a collection in java?

All standard implementations of collections List, Set and Map interface already implement java.io.Serializable. This means you do not really need to write anything specific to serialize collection objects. However you should keep following things in mind before you serialize a collection object - Make sure all the objects added in collection are Serializable.

A Does constructor get invoked when class is deserialized?

When an instance of a serializable class is deserialized, the constructor does not run. If constructor were invoked it will give back the initial values rather than the value they had at the time of serialization.

A Do you think that constructor will run in case where superclass is not serializable but subclass is?

Yes,If your subclass is a serializable class, but your super class is NOT serializable, then any instance variables you inherit from that superclass will be reset to the values they were given during the original construction of the object. This is because the non-serializable class constructor will run.

A To serialize an array or a collection all the members of it must be serializable. True /False?

True

A What is defaultWriteObject() method in Java?

The java.io.ObjectOutputStream.defaultWriteObject() method writes the non-static and non-transient fields of the current class to this stream. This may only be called from the writeObject method of the class being serialized. It will throw the NotActiveException if it is called otherwise.

A What is the difference between writeObject() and defaultWriteObject() method in serialization ?

  • writeObject is the most common method used to serialize the instance of class. defaultWriteObject method we use in custom or overridden writeObject method to do default processing of serialization.
  • When you call writeObject() method it doesn’t serialize the instances which are declared transient. So defaultWriteObject method is needed to customize the default serialization process.

A Does the order in which the value of the transient variables and the state of the object using the defaultWriteObject() method are saved during serialization matter?

Yes, while restoring the object’s state the transient variables and the serializable variables that are stored must be restored in the same order in which they were saved.