Java stream interview questions – 10 stream Interview Questions in Java

Java stream interview questions: List of topic-wise frequently asked java interview questions with the best possible answers for job interviews.

10 stream Interview Questions in Java

Question 1.
What is a stream and what are the types of Streams and classes of the Streams?
Answer:
A Stream is an abstraction that either produces or consumes information. There are two types of Streams and they are:
Byte Streams: Provide a convenient means for handling input and output of bytes.
Character Streams: Provide a convenient means for handling the input & output of characters.
Byte Streams classes: Are defined by using two abstract classes, namely InputStream and OutputStream.
Character Streams classes: Are defined by using two abstract classes, namely Reader and Writer.

Question 2.
What is the difference between Reader/Writer and InputStream/Output Stream?
Answer:
The Reader/Writer class is character-oriented and the InputStream/ OutputStream class is byte-oriented.

Question 3.
What is an I/O filter?
Answer:
An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another.

Question 4.
What are serialization and deserialization?
Answer:
Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.

Question 5.
What is object serialization?
Answer:
Serializing an object involves encoding its state in a structured way within a byte array. Once an object is serialized, the byte array can be manipulated in various ways; it can be written to a file, sent over a network using a socket-based connection or RMI, or persisted within a database as a BLOB.

The serialization process encodes enough information about the object type within the byte stream, allowing the original object to be easily recreated upon deserialization, at a later point in time.

Question 6.
Can I persist my objects using serialization instead of using a relational or object database?
Answer:
No. While serialization is a highly versatile mechanism having numerous applications, your long-term storage needs should continue to be addressed by conventional relational or object databases.

Note that serialization does not provide any features for transaction management and concurrency control. Nor does it provide typical database features like indexed access, caching, and a query language.

Question 7.
What are the security ramifications of using the Externalizable interface?
Answer:
The methods within the Externalizable interface, readExternal( ) and writeExternal( ) have public scope. This implies some client object could potentially bypass the Java sandbox mechanisms and overwrite or gain access to the state of an externalizable object.
As a general rule of thumb, a class should implement the Externalizable interface only if the object contains nonsensitive information.

Question 8.
Why am I having an InvalidClassException thrown during the serialization of my object which implements the Externalizable interface?
Answer:
Unlike objects which implement the Serializable interface, it is mandatory for objects implementing the Externalizable interface to also implement a public no-arg constructor. This constructor is the very first thing that is invoked by readExternal( ) when reconstructing the object from the byte stream. If a public no-arg constructor is absent, then an InvalidClassException is immediately thrown.

Question 9.
Why doesn’t serialization save the value of static variables?
Answer:
Variables declared as static members are not considered part of the state of an object because they are shared by all instances of that class. Classes that need to preserve the value of static members during serialization should save and restore these values explicitly using private void readObject(ObjectlnputStream) and private void writeObject(ObjectOutputStream).

Question 10.
What is the Stream Unique Identifier (SUID) that is written out as part of the serial stream?
Answer:
The serialization process uses a unique identification value to keep track of the persisted objects. When a Serializable or Externalizable object is saved, its fully- qualified class name and the Stream Unique Identifier (SUID) of the class are written out to the stream. The SUID is a unique 64-bit hash and is obtained by applying the SHA-1 message-digest algorithm to the serialized class, including its name, field types, and method signatures.

This step is important as it prevents the data persisted by one class from being read by another class with the same name. For any class to be able to read successfully from an object stream, it is imperative that its SUID matches the SUID of the serialized data in the stream.

Question 11.
What are the advantages and disadvantages of serialization?
Answer:
The advantages of serialization are:

  • It is easy to use and can be customized.
  • The serialized stream can be encrypted, authenticated, and compressed, supporting the needs of secure Java computing.
  • Serialized classes can support coherent versioning and are flexible enough to allow gradual evolution of your application’s object schema.
  • Serialization can also be used as a mechanism for exchanging objects between Java and C++ libraries, using third-party vendor libraries (like RogueWave’s Tools.h++) within C++.
  • There are simply too many critical technologies that rely upon serialization, including RMI, JavaBeans, and EJB.
    However, serialization has some disadvantages too:
  • It should ideally not be used with large-sized objects, as it offers significant overhead. Large objects also significantly increase the memory requirements of your application since the object input/output streams cache live references to all objects written to or read from the stream until the stream is closed or reset. Consequently, the garbage collection of these objects can be inordinately delayed.
  • The Serializable interface does not offer fine-grained control over object access – although you can somewhat circumvent this issue by implementing the complex Externalizable interface, instead.
  • Since serialization does not offer any transaction control mechanisms per se, it is not suitable for use within applications needing concurrent access without making use of additional APIs.

Question 12.
Does serialization depend on the browser, platform, or VM?
Answer:
The serialization format is independent of the browser, independent of the JVM vendor, and independent of the platform. So serialization should work with any combination of the above. For example, a serialized object written by Windows can be read by Unix, and vice versa. This is generally true of all I/O in Java.

Question 13.
If an object is serialized by a 1.1.x VM, can it be de-serialized by a 1.2.x VM?
Answer:
Yes. No special steps are needed for this to work.

Question 14.
What is a stream and what are the types of Streams and classes of the Streams?
Answer:
A Stream is an abstraction that either produces or consumes information. There are two types of Streams and they are:
Byte Streams: Provide a convenient means for handling input and output of bytes. Character Streams: Provide a convenient means for handling the input & output of characters.
Byte Streams classes: Are defined by using two abstract classes, namely InputStream and OutputStream.
Character Streams classes: Are defined by using two abstract classes, namely Reader and Writer.

Question 15.
What is the purpose of the File class?
Answer:
The File class is used to create objects that provide access to the files and directories of a local file system.

Question 16.
What class allows you to read objects directly from a stream?
Answer:
The ObjectlnputStream class supports the reading of objects from input streams.

Question 17.
What is the difference between the File and RandomAccessFile classes?
Answer:
The File class encapsulates the files and directories of the local file system. The RandomAccessFile class provides the methods needed to directly access data contained in any part of a file.

Question 18.
What interface must an object implement before it can be written to a stream as an object?
Answer:
An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object.

Question 19.
What is serialization?
Answer:
Serialization is a kind of mechanism that makes a class or bean persistence by having its properties or fields and state information saved and restored to and from storage.

Question 20.
How to make a class or a bean serializable?
Answer:
By implementing either the java.io.Serializable interface or the java. io.Externalizable interface. As long as one class in a class’s inheritance hierarchy implements Serializable or Externalizable, that class is serializable.

Question 21.
How many methods are in the Serializable interface?
Answer:
There is no method in the Serializable interface. The Serializable interface acts as a marker, telling the object serialization tools that your class is serializable.

Question 22.
How many methods are in the Externalizable interface?
Answer:
There are two methods in the Externalizable interface. You have to implement these two methods in order to make your class externalizable. These two methods are readExternal( ) and writeExternal( ).

Question 23.
What is the difference between Serializable and Externalizable interfaces?
Answer:
When you use the Serializable interface, your class is serialized automatically by default. But you can override write object( ) and read object( ) two methods to control more complex object serialization process. When you use the Externalizable interface, you have complete control over your class’s serialization process.

Question 24.
What is a transient variable?
Answer:
A transient variable is a variable that may not be serialized. If you don’t want some field to be serialized, you can mark that field as transient or static.

Question 25.
What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?
Answer:
The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.

Question 26.
What class allows you to read objects directly from a stream?
Answer:
The ObjectlnputStream class supports the reading of objects from input streams.

Question 27.
What interface must an object implement before it can be written to a stream as an object?
Answer:
An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object.

Question 28.
What is the difference between a Serializable and Externalizable interface? How can you control the serialization process Le? how can you customize the serialization process?
Answer:
When you use the Serializable interface, your class is serialized automatically by default. But you can override write object( ) and read object( ) two methods to control more complex object serialization process. When you use the Externalizable interface, you have complete control over your class’s serialization process. This interface contains two methods namely readExternal and writeExternal. You should implement these methods and write the logic for customizing the serialization process.

Question 29.
How to make a class or a bean serializable? How do I serialize an object to a file?
Or
What interface must an object implement before it can be written to a stream as an object?
Answer:
An object must implement the Serializable or Externalizable interface before it can be written to a stream as an object. The class whose instances are to be serialized should implement an interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected to a file output stream. This will save the object to a file.

Question 30.
What happens to the object references included in the object?
Answer:
The serialization mechanism generates an object graph for serialization. Thus it determines whether the included object references are serializable or not. This is a recursive process. Thus when an object is serialized, all the included objects are also serialized along with the original object.

Question 31.
What is serialization?
Answer:
Serialization is a kind of mechanism that makes a class or a bean persistent by having its properties or fields and state information saved and restored to and from storage. That is, it is a mechanism with which you can save the state of an object by converting it to a byte stream.

Common Usage of serialization.
Whenever an object is to be sent over the network or saved in a file, objects are serialized.

Question 32.
What happens to the static fields of a class during serialization?
Answer:
There are three exceptions in which serialization doesn’t necessarily read and write to the stream.
These are

  1. Serialization ignores static fields because they are not part of any particular state.
  2. Base class fields are only handled if the base class itself is serializable.
  3. Transient fields.

Question 33.
What one should take care of while serializing the object?
Answer:
One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.

Question 34.
What is a transient variable?
Or
Explain the usage of the keyword transient?
Or
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 as 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.

Question 35.
What is Externalizable?
Answer:
Externalizable is an interface that contains two methods read external( ) and write external( )- These methods give you control over the serialization mechanism. Thus if your class implements this interface, you can customize the serialization process by implementing these methods.

Question 36.
What happens to the static fields of a class during serialization?
Answer:
There are three exceptions in which serialization does not necessarily read and write to the stream. These are

  1. Serialization ignores static fields because they are not part of any particular state.
  2. Base class fields are only handled if the base class itself is serializable.
  3. Transient fields.