What will be the output of the following Java 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);
        }
        }
    }

a) 1
b) 2
c) 3
d) 4

1 thought on “What will be the output of the following Java program?”

  1. d
    Explanation: 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

Leave a Comment