Java Filter Streams - Quiz Explanation

The answers you selected are indicated below, along with text that explains the correct answers.
 
1. Which of the following is not a method in the PushbackInputStream class?
Please select the best answer.
  A. read()
  B. reset()
  C. close()
  D. toString()
  E. None of the above
 

The correct answer is E.

The read(), reset(), close(), and toString() methods are all valid methods that can be invoked by instances of PushbackInputStream. The reset() and close() methods are inherited from java.io.InputStream. The reset() method normally throws an IOException, but it's still a method of the class. The toString() method is inherited from java.lang.Object.

2. Besides creating a new instance of a PushbackInputStream, what else is the following line of code doing?
PushbackInputStream pis = new PushbackInputStream(System.in, 432);

Please select the best answer.
  A. Allows the unread() method to "unread" 432 bytes
  B. Allows marking and resetting for this PushbackInputStream
  C. Assigns the value 432 to the PushbackInputStream
  D. Compilation Fails
  E. A Runtime exception is thrown.
 

The correct answer is A.

You use this constructor to create a new instance of the PushbackInputStream pis with an "unread" buffer of 432 bytes.
Marking and resetting are allowed in a BufferedInputStream and in a ByteArrayInputStream, but not in a PushbackInputStream. You cannot assign a value to a PushbackInputStream by using a constructor.


3. Which of the following input stream classes always supports marking?
Please select the best answer.
  A. System.in
  B. DataInputStream
  C. FileInputStream
  D. ByteArrayInputStream
  E. None of the above
 

The correct answer is D.

The only two input stream classes in java.io that always support marking are BufferedInputStream and ByteArrayInputStream. However, other input streams like DataInputStream support marking when they're chained to a buffered input stream first.

4. Which of the following cannot be chained to a FileInputStream?
Please select the best answer.
  A. BufferedInputStream
  B. DataInputStream
  C. PrintStream
  D. PushbackInputStream
  E. None of the above
 

The correct answer is C.

PrintStream is an output stream, so it cannot be chained to any input stream, only to other output streams. The other three classes can all be chained to a FileInputStream.