Java IO Interview Questions

Java IO Interview Questions: Java Input/Output (IO) Streams are used to read and write data from input and output sources, such as files, network sockets, and standard input/output. Java IO Streams Interview Questions are an integral part of Java developer interviews, aimed at assessing a candidate’s knowledge and proficiency in using IO Streams for input and output operations in Java programming.

These interview questions evaluate a candidate’s understanding of IO classes and methods, file handling, exception handling, and stream types in Java programming. In this context, this article presents some commonly asked Java IO Streams Interview Questions to help Java developers prepare for interviews and better understand IO Streams in Java programming.

IO Streams Interview Questions

What value does read() return when it reaches the end of a file?
Ans: The read() method returns -1 when it reaches the end of a file.

What is serialization?
Ans: Serialization is a mechanism by which you can save the state of an object by converting it to a byte stream.

How do I serialize an object to a file?
Ans: 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.

Which methods of the Serializable interface should I implement?
Ans: The serializable interface is an empty interface; it does not contain any methods. So we do not implement any methods.

Can you customize the serialization process? If yes, then how can one have control over the serialization process?
Ans: Yes, it is possible to have control over the serialization process. For this, the class should implement the Externalizable interface. This interface contains two methods, namely, readExternal and writeExternal. You should implement these methods and write the logic for customizing the serialization process.

What is the common usage of serialization?
Ans: Whenever an object is to be sent over the network, objects need to be serialized. Moreover, if the state of an object is to be saved, objects need to be serialized.

What is Externalizable interface?
Ans: Externalizable is an interface which contains two methods readExternal and writeExternal. 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.

When can you serialize an object? What happens to the object references included in the object once you serialize them?
Ans: 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. When an object is serialized, all the included objects are also serialized along with the original object.

While serializing the object, that should be taken care of?
Ans: 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.

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

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

What is synchronization, and why is it important?
Ans: With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to significant errors.

How does Java handle integer overflows and underflows?
Ans: It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.

Does garbage collection guarantee that a program will not run out of memory?
Ans: Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.

What is the difference between preemptive scheduling and time slicing?
Ans: Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states, or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then re-enters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

What is the initial state of a thread when it is created and started?
Ans: A thread is in the ready state after it has been created and started.

What is a transient variable?
Ans: A transient variable is a variable that may not be serialized.

Why do threads block on I/O?
Ans: Threads block on I/O (that enters the waiting state) so that other threads may execute while the I/O operation is performed.

What value does readLine() return when it has reached the end of a file?
Ans: The readLine() method returns null when it has reached the end of a file.

When a thread blocks on I/O, what state does it enter?
Ans: A thread enters the waiting state when it blocks on I/O.

What value does read() return when it has reached the end of a file?
Ans: The read() method returns -1 when it has reached the end of a file.

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

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

What class allows you to read objects directly from a stream?
Ans: The ObjectInputStream class supports the reading of objects from input streams.

What an I/O filter?
Ans: 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.

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

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

What are Transient and Volatile Modifiers?
Ans: Transient Modifiers: The transient modifier applies to variables only, and it is not stored as part of its object’s Persistent state. Transient variables are not serialized. Volatile Modifiers: 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.

What is a stream, types of Streams and classes of Streams?
Ans: 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 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.

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

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

If you find any missed IO Stream interview questions, then you can update us by commenting on the comments section.

Ref: article

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

1 thought on “Java IO Interview Questions”

Leave a Comment