Question -
What is the output of this program?
import java.io.*;
class streams {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeFloat(3.5);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println(ois.available());
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
}
}
}
Answer: Option D oos.writeFloat(3.5); writes 3.5 in output stream. A new input stream is linked to stream 'serials',
an object 'ois' of ObjectInputStream is used to access this newly created stream, ois.available()
gives the total number of byte in the input stream since a float was written in the stream thus the
stream contains 4 byte, hence 4 is returned and printed.
Output:
$ javac streams.java
$ java streams
4
Was this answer helpful ?
Submit Your Solution hear: