Wednesday 27 February 2013

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

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


Choices:
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.

Answer:-
C is correct.
There is nothing wrong in writing to a FileOutputStream even after chaining a DataOutputStream onto it. C is correct as FileOutputStream writes 100 as a byte whereas the code tries to read it as integer. Of course A and D are incorrect (For the reasons just explained).

No comments:

Post a Comment