<%@ include file="../../ga4.jsp" %> Java InputStream [Reading] - Quiz Explanation

Reading Java Streams - Quiz Explanation

The answers you selected are indicated below, along with text that explains the correct answers.
 
1. Which one of the following statements about reading input streams is true?
Please select the best answer.
  A. When no bytes are available to be read, the available() method returns -1
  B. The InputStream class can never be instantiated directly
  C. All streams must be explicitly closed with the close() method
  D. read() returns a stream of bytes
  The correct answer is B.
Answer A is false. When no bytes are available to be read, the available() method returns 0. Ifread() encounters the end-of-stream, it returns -1. Answer C is false as well. Not all streams need to be explicitly closed using the close() method. For example,System.in generally does not need to be closed.

2. The maximum number of bytes that can be skipped past in one method call is:
Please select the best answer.
  A. 32,767
  B. 2,147,483,647
  C. 4,294,967,294
  D. 9,223,372,036,854,775,807
    The correct answer is D.
THe skip() method takes along as an argument. A Java long is an 8-byte, signed integer. Thus itsmaximum value, and the maximum number of bytes you can skip, is 263 - 1, or 9,223,372,036,854,775,807. Ifskip() took an int, then 2,147,483,647 would be the maximum value. Ifskip() took an unsigned int, then 4,294,967,294 would be the maximum value. Many older operating systems and APIs haveskip() equivalents that can only skip this far. For the most part, these APIs weredesigned in the 1970s and 1980s when really large hard disks and files were not yet common. The maximum size of a short is 32,767. However, Java's short, a 16-bit signed integer, is the standardint data type on some platforms like older Macs.

3. What data type does the read() method return?
Please select the best answer.
  A. byte
  B. int
  C. long
  D. short
  a The correct answer is B. The read() method returns an int in each of its three polymorphic variants. Although the int returned by thenoargs read() method represents an unsigned int with a value between 0 and 255, a byte cannot hold a positive value greater than 127 because bytes are signed.

4. Which decimal number indicates end-of-stream?
Please select the best answer.
  A. 0
  B. -1
  C. 4
  D. 28
  The correct answer is B.
The number -1 indicates end-of-stream. Since read() returns nonnegative numbers whenever a byte or bytes is successfully read, -1 is guaranteed not to appear in the data stream. The ASCII null character is 0. The ASCII EOT or "end of transmission" character is 4. The ASCII "file separator" character is 28. To the best of my knowledge neither of these control characters is in common use in modern operating systems. The numbers 0, 4, and 28 are all valid bytes that can appear in an input stream, so none of them can be used to indicate end-of-stream.