Java OCA OCP Practice Question 2458

Question

What's the output of the following code?

import java.io.*;
public class Main {
    public static void main(String args[]) throws IOException {
        DataOutputStream dos = new DataOutputStream(
                                   new FileOutputStream("contacts.txt"));
        dos.writeDouble(999.999);/*  w w  w . j a  v  a  2  s  .co  m*/
        DataInputStream dis = new DataInputStream(
                                   new FileInputStream("contacts.txt"));
        System.out.println(dis.read());
        System.out.println(dis.read());
        dis.close();
        dos.close();
    }
}
a  999.999/*from ww  w.jav  a  2s.c  o  m*/
   -1

b  999
   999

c  999.999
   EOFException

d  None of the above


d

Note

dos.writeDouble(999.999) writes 8 bytes of data to the underlying stream, and dis.read() reads a single byte of data from the underlying stream, interprets it as an integer value, and outputs it.

So the code neither prints 999.999 nor throws an EOFException.




PreviousNext

Related