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 
           {
           float x;
           FileInputStream fis = new FileInputStream("serial");
           ObjectInputStream ois = new ObjectInputStream(fis);
               x = ois.readInt();
               ois.close();
           System.out.println(x);		    	
       }
       catch (Exception e)
           {
               System.out.print("deserialization");
           System.exit(0);
       }
       }
   }

a) 3
b) 3.5
c) serialization
d) deserialization

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

  1. b
    Explanation: oos.writeFloat(3.5); writes in output stream which is extracted by x = ois.readInt(); and stored in x hence x contains 3.5.
    Output:
    $ javac streams.java
    $ java streams
    3.5

Leave a Comment