20 File Handling in Java Interview Questions and Answers

Prepare for the types of questions you are likely to be asked when interviewing for a position where File Handling in Java will be used.

When applying for a Java development position, you may be asked questions about file handling. This is a process of reading and writing data to and from files stored on a computer system. As a Java developer, it is important to have a strong understanding of how to handle files in order to efficiently and correctly read and write data. In this article, we will review some common file handling questions that you may encounter during a job interview.

File Handling in Java Interview Questions and Answers

Here are 20 commonly asked File Handling in Java interview questions and answers to prepare you for your interview:

1. What are the different ways of reading a file in Java?

There are several ways to read a file in Java. The most common way is to use the FileInputStream and BufferedReader classes. However, there are also other ways, such as using the Java NIO package or the Apache Commons IO library.

2. How do you read from and write to files using Java code?

You can read from and write to files using Java code by using the FileReader and FileWriter classes. These classes provide methods that allow you to read from and write to files, respectively. In order to use these classes, you must first import the package.

3. Can you explain what buffered input streams are?

A buffered input stream is a type of input stream that allows a programmer to read data from a stream in larger chunks, or buffers. This can be helpful in situations where a programmer wants to read a large amount of data at once, or when they want to read data from a stream that is slow or unreliable.

4. When should we use serialization? Where is it useful?

Serialization is the process of converting an object into a byte stream. This is useful when we need to store the state of an object or transmit it over a network. When we serialize an object, we can save it to a file or send it over the network. The byte stream can then be used to recreate the object on the other end.

5. What does the transient keyword do in Serialization?

The transient keyword is used in Serialization to indicate that a field should not be serialized. This is often used for fields that contain sensitive data that should not be stored in the serialized form of the object.

6. Is there a way to prevent certain fields from being serialized? If yes, then how?

Yes, there is a way to prevent certain fields from being serialized. This can be done by using the transient keyword. When a field is declared as transient, then it is not serialized.

7. What’s the usage of serialVersionUID in context with Serialization?

The serialVersionUID is a unique identifier for a particular version of a class. When a class is serialized, the serialVersionUID is used to ensure that the receiving party is compatible with the sender’s version of the class. If the serialVersionUIDs don’t match, then the receiving party will not be able to deserialize the object.

8. Is it possible to change an object after it has been deserialized? If yes, then how?

Yes, it is possible to change an object after it has been deserialized. This can be done by using the ObjectInputStream.enableResolveObject() method. This method will allow you to specify a callback object that will be used when an object is being deserialized. This callback object can then be used to resolve the object and make any necessary changes.

9. Can you give me some examples of when to use Serialization?

Serialization is the process of converting an object into a byte stream, which can be stored in a file or transmitted over a network. Some examples of when you might use Serialization would be if you were creating a backup of an object, or if you were sending an object over a network to another application.

10. What is the difference between deep cloning and shallow cloning?

Deep cloning is the process of creating an entirely new object that is a copy of an existing object. This new object will have all of the same properties and values as the original object. Shallow cloning, on the other hand, only creates a new object that references the same data as the original object. So, if you were to change a property on the new object, it would also change on the original object.

11. What is the significance of implementing Cloneable interface while creating a custom class?

The Cloneable interface is used as a marker interface, which indicates that the class which implements it is cloneable. This is important because the clone() method is a protected method in the Object class, and can only be accessed by subclasses of Object. Therefore, if a class wants to be cloneable, it must implement the Cloneable interface.

12. Which methods must be implemented by all subclasses of java.lang.Object?

The methods that must be implemented by all subclasses of java.lang.Object are: clone(), equals(), finalize(), and hashCode().

13. What do you understand about the Object class in Java?

The Object class is the superclass of all classes in Java. It provides methods for object creation and manipulation, and is the basis for the Object Oriented programming paradigm in Java.

14. Can you explain what the finalize() method does?

The finalize() method is a method that is called by the garbage collector when it determines that an object is no longer needed. This method can be used to clean up any resources that the object may be holding onto.

15. Why can’t you make a constructor private in Java?

In Java, a constructor must always be declared in the public access modifier. This is because the constructor is responsible for initializing the object, and if it were declared as private, then no other class would be able to create an instance of that object.

16. What will happen if you try to print an object of your own type without overriding its toString() method?

If you try to print an object of your own type without overriding its toString() method, then the object will be printed as its default string representation, which is typically the name of the object’s class followed by an @ sign and the object’s hash code.

17. What is the purpose of equals() and hashCode() functions in Java?

The equals() function is used to compare two objects to see if they are equal, while the hashCode() function is used to generate a numeric code that represents the object.

18. Can you explain what constructors are and their main uses?

Constructors are special methods that are used to initialize objects. A constructor is called when an object is created, and it can be used to set initial values for the object’s member variables.

19. What are immutable objects?

Immutable objects are those objects whose state cannot be changed once they are created. In Java, all primitive types (such as int, float, double, etc.) and String are immutable.

20. What are anonymous classes in Java? When would you use them?

Anonymous classes are classes that are not given a name. They are often used when you need to create a class that is only going to be used once and then discarded. For example, if you need to create a listener for a button press that is only going to be used that one time, you might create an anonymous class.


20 CSRF Interview Questions and Answers

Back to Interview

20 SAPUI5 Interview Questions and Answers