Connecting Java Streams - Quiz Explanation
The answers you selected are indicated below, along with text that explains the correct answers.
1.
Which of the following requires a separate stream object to provide raw data?
Please select the best answer.
A.
ByteArrayInputStream
B.
DataInputStream
C.
FileInputStream
D.
System.in
The correct answer is B.
A
DataInputStream
only reinterprets the raw bytes of data provided by another, underlying stream such as
System.in
, a
ByteArrayInputStream
, or a
FileInputStream
.
2.
Which of the following cannot be chained to a
FileOutputStream
?
Please select the best answer.
A.
BufferedOutputStream
B.
DataOutputStream
C.
PrintStream
D.
ByteArrayOutputStream
The correct answer is D.
ByteArrayOutputStream
does not extend
FilterOutputStream
, so it cannot be chained to nonfilter streams.
Semantically, a byte array is a destination like a file, not a filter or a buffer. If you were somehow able to chain a
FileOutputStream
to a
ByteArrayOutputStream
, the same data would end up in two different places.
3.
If you wanted a class that could write little-endian data, you would most likely extend:
Please select the best answer.
A.
FilterOutputStream
B.
FilterInputStream
C.
DataOutputStream
D.
OutputStream
The correct answer is A
.
FilterOutputStream
. Since you will want to read data from an underlying source and modify it before passing it on, you need a filter stream.
This is an output problem so Answer B is ruled out. You could extend
OutputStream
directly, but extending
FilterOutputStream
is easier since you don't have to implement
write()
.
Furthermore, other programmers who use this class will find it easier to understand. Similarly, you could extend
DataOutputStream
, but you don't need the extra methods of the
DataOutputStream
class. It's best to use
FilterOutputStream
for what it was meant for, exactly this sort of problem.