Java FPT Software (Fsoft) Academy Practice Test

Access More Questions
Which class is used to write objects to a file?
Correct Answer:
ObjectOutputStream
Explanation:
Serialization is the process of turning a Java object into a sequence of bytes so it can be saved to a file and later reconstructed. ObjectOutputStream is designed for this purpose: it writes the state of a Serializable object to an OutputStream, using methods like writeObject. When you pair it with a FileOutputStream, you can save the serialized object to a file. Other streams shown in the options write raw bytes (FileOutputStream, BufferedOutputStream) or human-readable text (PrintWriter), not the object’s full state, so they aren’t suitable for persisting objects. Remember that the class of the object must implement Serializable; otherwise an exception is raised during writing. In practice, you’d do something like new ObjectOutputStream(new FileOutputStream("data.ser")) and call writeObject(yourObject) before closing.

Access more questions from this quiz

Continue to Java FPT Software (Fsoft) Academy Practice Test for more practice questions and the full quiz experience.

Access More Questions