The IT Certification Resource Center

Featured Deal

Get CompTIA, Cisco, and Microsoft training courses free for a week.
Learn More ❯

Question 20:

What results from trying to compile and run the following code?

1.  import java.io.*;
2.
3.  class MyClass
4.  {
5.      public static void main(String args[])
6.      {
7.          try
8.          {
9.              FileOutputStream fos = new FileOutputStream("abc");
10.             DataOutputStream dos = new DataOutputStream(fos);
11.             dos.writeByte(12);
12.             fos.write(100);
13.             fos.close();
14.             dos.close();
15.
16.             FileInputStream fis = new FileInputStream("abc");
17.             DataInputStream dis = new DataInputStream(fis);
18.             byte b = dis.readByte();
19.             System.out.print(b + " ");
20.             int i = dis.readInt();
21.             System.out.println(i);
22.             fis.close();
23.             dis.close();
24.         }
25.         catch(IOException e)
26.         {
27.             System.out.println("An exception occurred");
28.         }
29.     }
30. }
A. The output is 12 100
B. Compilation error at line 12 because once you chain a DataOutputStream onto the FileOutputStream, you can't write directly to the FileOutputStream.
C. An exception occurs at Run time at line 20 because there are only two bytes written in the file "abc" and the code tries to read a byte and then an integer.
D. Compilation error occurs at line 20 because there are only two bytes written in the file "abc" and the code tries to read a byte and then an integer.