Multiple Choice Questions 6

  1. InputStream and OutputSteam are _______________.

  2. The top-level abstract classes defined in the java.io package, from which all other stream classes inherit.
    The top-level abstract classes defined in the java.io package, from which all other byte stream classes inherit.
    Classes that you can instantiate to read and write bytes.
    Interfaces that define methods that can be used to read and write bytes.

  3. What does the following code do?

  4. FileInputStream fis = new FileInputStream("test.dat");
    It creates a new file named test.dat if it does not exist and opens the file so you can write to it.
    It creates a new file named test.dat if it does not exist and opens the file so you can write to it and read from it.
    It creates a new file named test.dat regardless of whether it exists or not and opens the file so you can write to it.
    It creates a new file named test.dat regardless of whether it exists or not and opens the file so you can write to it and read from it.

  5. Which of the following statements is correct to create a DataOutputStream to write to a file named out.dat?

  6. DataOutputStream outfile = new DataOutputStream(new File("out.dat"));
    DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat"));
    DataOutputStream outfile = new DataOutputStream(FileOutputStream("out.dat"));
    DataOutputStream outfile = new DataOutputStream("out.dat");

  7. Which of the following statements is correct to create a BufferedWriter stream to write to a file named out.dat?

  8. BufferedWriter bw = new BufferedWriter(new File("out.dat"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("out.dat"));
    BufferedWriter bw = new BufferedWriter("out.dat");
    BufferedWriter bw = new BufferedWriter(new FileOutputStream("out.dat"));

  9. To create a file, you can use _______________.

  10. FileOutputStream
    FileWriter
    RandomAccessFile
    All of the above

  11. Which of the following statements is incorrect to create a StreamTokenizer stream to read a file named in.dat?

  12. StreamTokenizer st = new StreamTokenizer(new FileInputStream("in.dat"));
    StreamTokenizer st = new StreamTokenizer(new FileInputStream(new File("in.dat")));
    StreamTokenizer st = new StreamTokenizer(new FileReader(new File("in.dat")));
    StreamTokenizer st = new StreamTokenizer(new FileReader("in.dat"));

  13. Which of the following is the legal mode for creating a new RandomAccessFile stream?

  14. "w"
    'r'
    "rw"
    "rwx"

  15. What happens if the file test.dat does not exist when you attempt to compile and run the following code?

  16. import java.io.*;
    class Test
    { public static void main(String[] args)
    { try
    { RandomAccessFile raf = new RandomAccessFile("test.dat", "r");
    int i = raf.readInt();
    }
    catch(IOException e)
    { System.out.println("runtime exception"});
    }
    }

    The program does not compile because raf is not created correctly.
    The program does not compile because readInt() is not implemented in RandomAccessFile.
    The program compiles, but throws IOException because the file test.dat doesn't exist. The program displays runtime exception.
    The program compiles and runs fine, but nothing is displayed on the console.

  17. Which type of exception occurs when creating a FileInputStream for a nonexistent file?

  18. FileNotExist
    FileNotExistException
    FileNotFound
    FileNotFoundException

  19. Which of the following methods cannot be invoked by an instance of StreamTokenizer?

  20. nextToken()
    toString()
    equals()
    close()

Visited times since 7 March 1999.