Java Input/Output  «Prev   Next»

Java DataStreams - Quiz

Each question is worth one point. Select the best answer or answers for each question.
1. Which concrete class wraps an InputStream and provides methods such as readInt() and readDouble() to read primitive values stored in Java's data-stream binary format?
Please select the correct answer:
  A. DataInput
  B. DataInputStream
  C. InputStream

2. Assuming java.io.DataInputStream is imported, which expression correctly constructs a DataInputStream that wraps standard input?
Please select the correct answer:
  A. new DataInputStream(System.out)
  B. new DataInputStream("in")
  C. new DataInputStream(System.in)

3. Given a BufferedReader named dataIn and a declared String s, which statement reads the next line into s? Assume IOException is handled or declared.
Please select the correct answer:
  A. s = dataIn.readLine();
  B. s = dataIn.read();
  C. s.readLine();

4. A DataOutputStream writes a record using out.writeInt(42); out.writeDouble(3.5); in that order. Which sequence correctly reads the record using a DataInputStream named in? Assume the stream is positioned at the record's start and IOException is handled or declared.
Please select the correct answer:
  A. double value = in.readDouble(); int count = in.readInt();
  B. int count = in.readInt(); double value = in.readDouble();
  C. int count = in.read(); double value = in.read();

5. What happens when DataInputStream.readInt() reaches end-of-input before it can read all four bytes required for an int?
Please select the correct answer:
  A. It returns -1.
  B. It pads the missing bytes with zeros.
  C. It throws java.io.EOFException.