Java MCQ Set 1
1. Which exception is thrown by read() method?
a) IOException
b) InterruptedException
c) SystemException
d) SystemInputException
Answer
Answer: a [Reason:] read method throws IOException.
2. Which of these is used to read a string from the input stream?
a) get()
b) getLine()
c) read()
d) readLine()
Answer
Answer: c [Reason:] None.
3. Which of these class is used to read characters and strings in Java from console?
a) BufferedReader
b) StringReader
c) BufferedStreamReader
d) InputStreamReader
Answer
Answer: a [Reason:] None.
4. Which of these classes are used by Byte streams for input and output operation?
a) InputStream
b) InputOutputStream
c) Reader
d) All of the mentioned
Answer
Answer: a [Reason:] Byte stream uses InputStream and OutputStream classes for input and output operation.
5. Which of these class is implemented by FilterInputStream class?
a) InputStream
b) InputOutputStream
c) BufferedInputStream
d) SequenceInputStream
Answer
Answer: a [Reason:] FileInputStream implements InputStream.
6. Which of these class is used to read from a file?
a) InputStream
b) BufferedInputStream
c) FileInputStream
d) BufferedFileInputStream
Answer
Answer: c [Reason:] None.
7. What is the output of this program if input given is “Hello stop World”?
-
class Input_Output
-
{
-
public static void main(String args[]) throws IOException
-
{
-
string str;
-
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
-
do
-
{
-
str = (char) obj.readLine();
-
System.out.print(str);
-
} while(!str.equals("strong"));
-
}
-
}
a) Hello
b) Hello stop
c) World
d) Hello stop World
Answer
Answer: d [Reason:] “stop” will be able to terminate the do-while loop only when it occurs singly in a line. “Hello stop World” does not terminate the loop.
Output:
$ javac Input_Output.java
$ java Input_Output
Hello stop World
8. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer c = new StringBuffer("Hello");
-
StringBuffer c1 = new StringBuffer(" World");
-
c.append(c1);
-
System.out.println(c);
-
}
-
}
a) Hello
b) World
c) Helloworld
d) Hello World
Answer
Answer: d [Reason:] append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
Output:
$ javac output.java
$ java output
Hello World
9. What is the output of this program?
-
class output
-
{
-
public static void main(String args[])
-
{
-
StringBuffer s1 = new StringBuffer("Hello");
-
s1.setCharAt(1,x);
-
System.out.println(s1);
-
}
-
}
a) xello
b) xxxxx
c) Hxllo
d) Hexlo
Answer
Answer: c [Reason:] None.
Output:
$ javac output.java
$ java output
Hxllo
10. What is the output of this program if input given is “abc’def/’egh”?
-
class Input_Output
-
{
-
public static void main(String args[]) throws IOException
-
{
-
char c;
-
BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
-
do
-
{
-
c = (char) obj.read();
-
System.out.print(c);
-
} while(c != ''');
-
}
-
}
a) abc’
b) abcdef/’
c) abc’def/’egh
d) abcqfghq
Answer
Answer: a [Reason:] ’ is used for single quotes that is for representing ‘ .
Output:
$ javac Input_Output.java
$ java Input_Output
abc’
Java MCQ Set 2
1. Which of these class contains the methods used to write in a file?
a) FileStream
b) FileInputStream
c) BUfferedOutputStream
d) FileBufferStream
Answer
Answer: b [Reason:] None.
2. Which of these exception is thrown in cases when the file specified for writing it not found?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputException
Answer
Answer: c [Reason:] In cases when the file specified is not found, then FileNotFoundException is thrown by java run-time system, earlier versions of java used to throw IOException but after Java 2.0 they throw FileNotFoundException.
3. Which of these methods are used to read in from file?
a) get()
b) read()
c) scan()
d) readFileInput()
Answer
Answer: b [Reason:] Each time read() is called, it reads a single byte from the file and returns the byte as an integer value. read() returns -1 when the end of the file is encountered.
4. Which of these values is returned by read() method is end of file (EOF) is encountered?
a) 0
b) 1
c) -1
d) Null
Answer
Answer: c [Reason:] Each time read() is called, it reads a single byte from the file and returns the byte as an integer value. read() returns -1 when the end of the file is encountered.
5. Which of these exception is thrown by close() and read() methods?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputOutputException
Answer
Answer: a [Reason:] Both close() and read() method throw IOException.
6. Which of these methods is used to write() into a file?
a) put()
b) putFile()
c) write()
d) writeFile()
Answer
Answer: c [Reason:] None.
7. What is the output of this program?
-
import java.io.*;
-
class filesinputoutput
-
{
-
public static void main(String args[])
-
{
-
InputStream obj = new FileInputStream("inputoutput.java");
-
System.out.print(obj.available());
-
}
-
}
Note: inputoutput.java is stored in the disk.
a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file
Answer
Answer: c [Reason:] obj.available() returns the number of bytes.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
1422
(Output will be different in your case)
8. What is the output of this program?
-
import java.io.*;
-
public class filesinputoutput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abc";
-
byte b[] = obj.getBytes();
-
ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
-
for (int i = 0; i < 2; ++ i)
-
{
-
int c;
-
while((c = obj1.read()) != -1)
-
{
-
if(i == 0)
-
{
-
System.out.print(Character.toUpperCase((char)c));
-
obj2.write(1);
-
}
-
}
-
System.out.print(obj2);
-
}
-
}
-
}
a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa
Answer
Answer: d [Reason:] None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa
9. What is the output of this program?
-
import java.io.*;
-
class Chararrayinput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abcdef";
-
int length = obj.length();
-
char c[] = new char[length];
-
obj.getChars(0, length, c, 0);
-
CharArrayReader input1 = new CharArrayReader(c);
-
CharArrayReader input2 = new CharArrayReader(c, 0, 3);
-
int i;
-
try
-
{
-
while((i = input2.read()) != -1)
-
{
-
System.out.print((char)i);
-
}
-
}
-
catch (IOException e)
-
{
-
e.printStackTrace();
-
}
-
}
-
}
a) abc
b) abcd
c) abcde
d) abcdef
Answer
Answer: a [Reason:] None.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
abc
10. What is the output of this program?
-
import java.io.*;
-
class Chararrayinput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abcdefgh";
-
int length = obj.length();
-
char c[] = new char[length];
-
obj.getChars(0, length, c, 0);
-
CharArrayReader input1 = new CharArrayReader(c);
-
CharArrayReader input2 = new CharArrayReader(c, 1, 4);
-
int i;
-
int j;
-
try
-
{
-
while((i = input1.read()) == (j = input2.read()))
-
{
-
System.out.print((char)i);
-
}
-
}
-
catch (IOException e)
-
{
-
e.printStackTrace();
-
}
-
}
-
}
a) abc
b) abcd
c) abcde
d) none of the mentioned
Answer
Answer: d [Reason:] No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2 contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting character of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
Java MCQ Set 3
1. What is Remote method invocation (RMI)?
a) RMI allows us to invoke a method of java object that executes on another machine
b) RMI allows us to invoke a method of java object that executes on another Thread in multithreaded programming
c) RMI allows us to invoke a method of java object that executes parallely in same machine
d) None of the mentioned
Answer
Answer: a [Reason:] Remote method invocationor RMI allows us to invoke a method of java object that executes on another machine..
2. Which of these package is used for remote method invocation?
a) java.applet
b) java.rmi
c) java.lang.rmi
d) java.lang.reflect
Answer
Answer: b [Reason:] None.
3. Which of these methods are member of Remote class?
a) checkIP()
b) addLocation()
c) AddServer()
d) None of the mentioned
Answer
Answer: d [Reason:] Remote class does not define any methods, its pupose is simply to indicate that an interface uses remote methods.
4. Which of these Exceptions is thrown by remote method?
a) RemoteException
b) InputOutputException
c) RemoteAccessException
d) RemoteInputOutputException
Answer
Answer: a [Reason:] All remote methods throw RemoteException.
5. Which of these class is used for creating a client for a server-client operations?
a) serverClientjava
b) Client.java
c) AddClient.java
d) ServerClient.java
Answer
Answer: c [Reason:] None.
6. Which of these package is used for all the text related modifications?
a) java.text
b) java.awt
c) java.lang.text
d) java.text.mofify
Answer
Answer: a [Reason:] java.text provides capabilities for formatting, searching and manipulating text.
7. What is the output of this program?
-
import java.lang.reflect.*;
-
class Additional_packages
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
Class c = Class.forName("java.awt.Dimension");
-
Constructor constructors[] = c.getConstructors();
-
for (int i = 0; i < constructors.length; i++)
-
System.out.println(constructors[i]);
-
}
-
catch (Exception e)
-
{
-
System.out.print("Exception");
-
}
-
}
-
}
a) Program prints all the constructors of ‘java.awt.Dimension’ package
b) Program prints all the possible constructors of class ‘Class’
c) Program prints “Exception”
d) Runtime Error
Answer
Answer: a [Reason:] None.
Output:
$ javac Additional_packages.java
$ java Additional_packages
public java.awt.Dimension(java.awt.Dimension)
public java.awt.Dimension()
public java.awt.Dimension(int,int)
8. What is the output of this program?
-
import java.lang.reflect.*;
-
class Additional_packages
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
Class c = Class.forName("java.awt.Dimension");
-
Field fields[] = c.getFields();
-
for (int i = 0; i < fields.length; i++)
-
System.out.println(fields[i]);
-
}
-
catch (Exception e)
-
{
-
System.out.print("Exception");
-
}
-
}
-
}
a) Program prints all the constructors of ‘java.awt.Dimension’ package
b) Program prints all the methods of ‘java.awt.Dimension’ package
c) Program prints all the data members of ‘java.awt.Dimension’ package
d) program prints all the methods and data member of ‘java.awt.Dimension’ package
Answer
Answer: c [Reason:] None.
Output:
$ javac Additional_packages.java
$ java Additional_packages
public int java.awt.Dimension.width
public int java.awt.Dimension.height
9. What is the length of the application box made by this program?
-
import java.awt.*;
-
import java.applet.*;
-
public class myapplet extends Applet
-
{
-
Graphic g;
-
g.drawString("A Simple Applet",20,20);
-
}
a) 20
b) Default value
c) Compilation Error
d) Runtime Error
Answer
Answer: c [Reason:] To implement the method drawString we need first need to define abstract method of AWT that is paint() method. Without paint() method we cannot define and use drawString or any Graphic class methods.
10. What is the output of this program?
-
import java.lang.reflect.*;
-
class Additional_packages
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
Class c = Class.forName("java.awt.Dimension");
-
Method methods[] = c.getMethods();
-
for (int i = 0; i < methods.length; i++)
-
System.out.println(methods[i]);
-
}
-
catch (Exception e)
-
{
-
System.out.print("Exception");
-
}
-
}
-
}
a) Program prints all the constructors of ‘java.awt.Dimension’ package
b) Program prints all the methods of ‘java.awt.Dimension’ package
c) Program prints all the data members of ‘java.awt.Dimension’ package
d) program prints all the methods and data member of ‘java.awt.Dimension’ package
Answer
Answer: b [Reason:] None.
Output:
$ javac Additional_packages.java
$ java Additional_packages
public int java.awt.Dimension.hashCode()
public boolean java.awt.Dimension.equals(java.lang.Object)
public java.lang.String java.awt.Dimension.toString()
public java.awt.Dimension java.awt.Dimension.getSize()
public void java.awt.Dimension.setSize(double,double)
public void java.awt.Dimension.setSize(int,int)
public void java.awt.Dimension.setSize(java.awt.Dimension)
public double java.awt.Dimension.getHeight()
public double java.awt.Dimension.getWidth()
public java.lang.Object java.awt.geom.Dimension2D.clone()
public void java.awt.geom.Dimension2D.setSize(java.awt.geom.Dimension2D)
public final native java.lang.Class java.lang.Object.getClass()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()
public final native void java.lang.Object.wait(long)
public final void java.lang.Object.wait(long,int)
public final void java.lang.Object.wait()
Java MCQ Set 4
1. Which of these types cannot be used to initiate a generic type?
a) Integer class
b) Float class
c) Primitive Types
d) Collections
Answer
Answer: c [Reason:] None.
2. Which of these instance cannot be created?
a) Integer instance
b) Generic class instance
c) Generic type instance
d) Collection instances
Answer
Answer: c [Reason:] It is not possible to create generic type instances. Example – “E obj = new E()” will give a compilation error.
3. Which of these data type cannot be type parameterized?
a) Array
b) List
c) Map
d) Set
Answer
Answer: a [Reason:] None.
4. Which of these Exception handlers cannot be type parameterized?
a) catch
b) throw
c) throws
d) all of the mentioned
Answer
Answer: d [Reason:] we cannot Create, Catch, or Throw Objects of Parameterized Types as generic class cannot extend the Throwable class directly or indirectly.
5. Which of the following cannot be Type parameterized?
a) Oveloaded Methods
b) Generic methods
c) Class methods
d) Overriding methods
Answer
Answer: a [Reason:] Cannot Overload a Method Where the Formal Parameter Types of Each Overload Erase to the Same Raw Type.
6. What is the output of this program?
-
public class BoxDemo
-
{
-
public static <U> void addBox(U u,
-
java.util.List<Box<U>> boxes)
-
{
-
Box<U> box = new Box<>();
-
box.set(u);
-
boxes.add(box);
-
}
-
public static <U> void outputBoxes(java.util.List<Box<U>> boxes)
-
{
-
int counter = 0;
-
for (Box<U> box: boxes)
-
{
-
U boxContents = box.get();
-
System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]");
-
counter++;
-
}
-
}
-
public static void main(String[] args)
-
{
-
java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>();
-
BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
-
BoxDemo.outputBoxes(listOfIntegerBoxes);
-
}
-
}
a) 10
b) Box #0 [10].
c) Box contains [10].
d) Box #0 contains [10].
Answer
Answer: d [Reason:] None.
Output:
$ javac Output.java
$ java Output
Box #0 contains [10]
7. What is the output of this program?
-
import java.util.*;
-
public class genericstack <E>
-
{
-
Stack <E> stk = new Stack <E>();
-
public void push(E obj)
-
{
-
stk.push(obj);
-
}
-
public E pop()
-
{
-
E obj = stk.pop();
-
return obj;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
genericstack <String> gs = new genericstack<String>();
-
gs.push("Hello");
-
System.out.print(gs.pop() + " ");
-
genericstack <Integer> gs = new genericstack<Integer>();
-
gs.push(36);
-
System.out.println(gs.pop());
-
}
-
}
a) Error
b) Hello
c) 36
d) Hello 36
Answer
Answer: d [Reason:] None.
Output:
$ javac Output.java
$ java Output
Hello 36
8. What is the output of this program?
-
import java.util.*;
-
class Output
-
{
-
public static double sumOfList(List<? extends Number> list)
-
{
-
double s = 0.0;
-
for (Number n : list)
-
s += n.doubleValue();
-
return s;
-
}
-
public static void main(String args[])
-
{
-
List<Double> ld = Arrays.asList(1.2, 2.3, 3.5);
-
System.out.println(sumOfList(ld));
-
}
-
}
a) 5.0
b) 7.0
c) 8.0
d) 6.0
Answer
Answer: b [Reason:] None.
Output:
$ javac Output.java
$ java Output
7.0
9. What is the output of this program?
-
import java.util.*;
-
class Output
-
{
-
public static void addNumbers(List<? super Integer> list)
-
{
-
for (int i = 1; i <= 10; i++)
-
{
-
list.add(i);
-
}
-
}
-
public static void main(String args[])
-
{
-
List<Double> ld = Arrays.asList();
-
addnumbers(10.4);
-
System.out.println("getList(2)");
-
}
-
}
a) 1
b) 2
c) 3
d) 6
Answer
Answer: a [Reason:] None.
Output:
$ javac Output.java
$ java Output
1
10. What is the output of this program?
-
import java.util.*;
-
public class genericstack <E>
-
{
-
Stack <E> stk = new Stack <E>();
-
public void push(E obj)
-
{
-
stk.push(obj);
-
}
-
public E pop()
-
{
-
E obj = stk.pop();
-
return obj;
-
}
-
}
-
class Output
-
{
-
public static void main(String args[])
-
{
-
genericstack <Integer> gs = new genericstack<Integer>();
-
gs.push(36);
-
System.out.println(gs.pop());
-
}
-
}
a) H
b) Hello
c) Runtime Error
d) Compilation Error
Answer
Answer: d [Reason:] genericstack’s object gs is defined to contain a string parameter but we are sending an integer parameter, which results in compilation error.
Output:
$ javac Output.java
$ java Output
Hello
Java MCQ Set 5
1. Which of these is a process of writing the state of an object to a byte stream?
a) Serialization
b) Externalization
c) File Filtering
d) All of the mentioned
Answer
Answer: a [Reason:] Serialization is the process of writing the state of an object to a byte stream. This is used when you want to save the state of your program to persistent storage area.
2. Which of these process occur automatically by java run time system?
a) Serialization
b) Garbage collection
c) File Filtering
d) All of the mentioned
Answer
Answer: a [Reason:] Serialization and deserialization occur automatically by java run time system, Garbage collection also occur automatically but is done by CPU or the operating system not by the java run time system.
3. Which of these is an interface for control over serialization and deserialization?
a) Serializable
b) Externalization
c) FileFilter
d) ObjectInput
Answer
Answer: b [Reason:] None.
4. Which of these interface extends DataOutput interface?
a) Serializable
b) Externalization
c) ObjectOutput
d) ObjectInput
Answer
Answer: c [Reason:] ObjectOutput interface extends the DataOutput interface and supports object serialization.
5. Which of these is a method of ObjectOutput interface used to finalize the output state so that any buffers are cleared?
a) clear()
b) flush()
c) fflush()
d) close()
Answer
Answer: b [Reason:] None.
6. Which of these is method of ObjectOutput interface used to write the object to input or output stream as required?
a) write()
b) Write()
c) StreamWrite()
d) writeObject()
Answer
Answer: d [Reason:] writeObject() is used to write an object into invoking stream, it can be input stream or output stream.
7. What is the output of this program?
-
import java.io.*;
-
class serialization
-
{
-
public static void main(String[] args)
-
{
-
try
-
{
-
Myclass object1 = new Myclass("Hello", -7, 2.1e10);
-
FileOutputStream fos = new FileOutputStream("serial");
-
ObjectOutputStream oos = new ObjectOutputStream(fos);
-
oos.writeObject(object1);
-
oos.flush();
-
oos.close();
-
}
-
catch(Exception e)
-
{
-
System.out.println("Serialization" + e);
-
System.exit(0);
-
}
-
try
-
{
-
Myclass object2;
-
FileInputStream fis = new FileInputStream("serial");
-
ObjectInputStream ois = new ObjectInputStream(fis);
-
object2 = (Myclass)ois.readObject();
-
ois.close();
-
System.out.println(object2);
-
}
-
catch (Exception e)
-
{
-
System.out.print("deserialization" + e);
-
System.exit(0);
-
}
-
}
-
}
-
class Myclass implements Serializable
-
{
-
String s;
-
int i;
-
double d;
-
Myclass (String s, int i, double d)
-
{
-
this.d = d;
-
this.i = i;
-
this.s = s;
-
}
-
}
a) s=Hello; i=-7; d=2.1E10
b) Hello; -7; 2.1E10
c) s; i; 2.1E10
d) Serialization
Answer
Answer: a [Reason:] None.
Output:
$ javac serialization.java
$ java serialization
s=Hello; i=-7; d=2.1E10
8. What is the output of this program?
-
import java.io.*;
-
class serialization
-
{
-
public static void main(String[] args)
-
{
-
try
-
{
-
Myclass object1 = new Myclass("Hello", -7, 2.1e10);
-
FileOutputStream fos = new FileOutputStream("serial");
-
ObjectOutputStream oos = new ObjectOutputStream(fos);
-
oos.writeObject(object1);
-
oos.flush();
-
oos.close();
-
}
-
catch(Exception e)
-
{
-
System.out.println("Serialization" + e);
-
System.exit(0);
-
}
-
try
-
{
-
int 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);
-
}
-
}
-
}
-
class Myclass implements Serializable
-
{
-
String s;
-
int i;
-
double d;
-
Myclass(String s, int i, double d)
-
{
-
this.d = d;
-
this.i = i;
-
this.s = s;
-
}
-
}
a) -7
b) Hello
c) 2.1E10
d) deserialization
Answer
Answer: d [Reason:] x = ois.readInt(); will try to read an integer value from the stream ‘serial’ created before, since stream contains an object of Myclass hence error will occur and it will be catched by catch printing deserialization.
Output:
$ javac serialization.java
$ java serialization
deserialization
9. What is the output of this program?
-
import java.io.*;
-
class Chararrayinput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abcdefgh";
-
int length = obj.length();
-
char c[] = new char[length];
-
obj.getChars(0, length, c, 0);
-
CharArrayReader input1 = new CharArrayReader(c);
-
CharArrayReader input2 = new CharArrayReader(c, 1, 4);
-
int i;
-
int j;
-
try
-
{
-
while ((i = input1.read()) == (j = input2.read()))
-
{
-
System.out.print((char)i);
-
}
-
}
-
catch (IOException e)
-
{
-
e.printStackTrace();
-
}
-
}
-
}
a) abc
b) abcd
c) abcde
d) None of the mentioned
Answer
Answer: d [Reason:] No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2 contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting character of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
10. 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
-
{
-
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
Answer
Answer: b [Reason:] 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