Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 11. Files and Streams > Review Questions

Review Questions

11.1Which of these can act both as the source of an input stream and as the destination of an output stream, based on the classes provided by the java.io package?

Select the four correct answers.

  1. A file

  2. A network connection

  3. A pipe

  4. A string

  5. An array of chars

11.2Which of these statements about the constant named separator of the File class are true?

Select the two correct answers.

  1. The variable is of type char.

  2. The variable is of type String.

  3. It can be assumed that the value of the variable always is the character '/'.

  4. It can be assumed that the value of the variable always is one of '/', '\' or ':'.

  5. The separator can consist of more than one character.

11.3Which one of these methods in the File class will return the name of the entry, excluding the specification of the directory in which it resides?

Select the one correct answer.

  1. getAbsolutePath()

  2. getName()

  3. getParent()

  4. getPath()

  5. None of the above.

11.4What will the method length() in the class File return?

Select the one correct answer.

  1. The number of characters in the file.

  2. The number of kilobytes in the file.

  3. The number of lines in the file.

  4. The number of words in the file.

  5. None of the above.

11.5Given the following program:
import java.io.File;
import java.io.IOException;

public final class Filing {
  public static void main (String[] args) throws IOException {
    File file = new File("./documents","../book/../chapter1");
    System.out.println(file.getPath());
    System.out.println(file.getAbsolutePath());
    System.out.println(file.getCanonicalPath());
    System.out.println(file.getName());
    System.out.println(file.getParent());
  }
}

Assume that the current or working directory has the absolute path "/wrk". Which lines below will not be included in the output from the program?

Select the two correct answers.

  1. ./documents/../book/../chapter1

  2. ./documents/book/chapter1

  3. /wrk/./documents/../book/../chapter1

  4. /wrk/documents/book/chapter1

  5. /wrk/chapter1

  6. chapter1

  7. ./documents/../book/..

11.6Given the following program:
import java.io.File;
public class ListingFiles {
  public static void main(String[] args) {
    File currentDirectory = new File(".");
    printFiles1(currentDirectory);
    printFiles2(currentDirectory);
    printFiles3(currentDirectory);
  }

  public static void printFiles1(File currentDirectory) {
    String[] entryNames = currentDirectory.list();
    for (String entryName : entryNames) {
      System.out.println(entryName);
    }
  }

  public static void printFiles2(File currentDirectory) {
    File[] entries = currentDirectory.listFiles();
    for (File entry : entries) {
      System.out.println(entry);
    }
  }

  public static void printFiles3(File currentDirectory) {
    File[] entries = currentDirectory.listFiles();
    for (File entry : entries) {
      System.out.println(entry.getPath());
    }
  }
}

					  

Assume that the current or working directory has the absolute path "/wrk" and contains only one file with the name "ListingFiles.class".

Which statement is true about the program?

Select the one correct answer.

  1. All three methods printFiles1(), printFiles2(), and printFiles3() will produce the same output.

  2. Only the methods printFiles1() and printFiles2(), will produce the same output.

  3. Only the methods printFiles2() and printFiles3(), will produce the same output.

  4. Only the methods printFiles1() and printFiles3(), will produce the same output.

  5. The program does not compile because the list() method does not exist in the File class.

11.7A file is readable but not writable on the file system of the host platform. What will be the result of calling the method canWrite() on a File object representing this file?

Select the one correct answer.

  1. A SecurityException is thrown.

  2. The boolean value false is returned.

  3. The boolean value true is returned.

  4. The file is modified from being unwritable to being writable.

  5. None of the above.

11.8What is the type of the parameter given to the method renameTo() in the class File?

Select the one correct answer.

  1. File

  2. FileDescriptor

  3. FileNameFilter

  4. String

  5. char[]

11.9If write(0x01234567) is called on an instance of OutputStream, what will be written to the destination of the stream?

Select the one correct answer.

  1. The bytes 0x01, 0x23, 0x34, 0x45, and 0x67, in that order.

  2. The bytes 0x67, 0x45, 0x34, 0x23, and 0x01, in that order.

  3. The byte 0x01.

  4. The byte 0x67.

  5. None of the above.

11.10Given the following code, under which circumstances will the method return false?
public static boolean test(InputStream is) throws IOException {
  int value = is.read();
  return value >= 0;
}

Select the one correct answer.

  1. A character of more than 8 bits was read from the input stream.

  2. An I/O error occurred.

  3. Never.

  4. The end of the stream was reached in the input stream.

11.11Which of these classes provides methods for writing binary representations of Java primitive values?

Select the two correct answers.

  1. DataOutputStream

  2. FileOutputStream

  3. ObjectOutputStream

  4. PrintStream

  5. BufferedOutputStream

11.12Given the following program:
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Endings {
  public static void main(String[] args) {
    try {
      FileInputStream fos = new FileInputStream("info.dat");
      DataInputStream dis = new DataInputStream(fos);
      int i = dis.readByte();
      while (i != -1) {
        System.out.print((byte)i + "|");
        i = dis.readByte();
      }
    } catch (FileNotFoundException fnf) {
      System.out.println("File not found");
    } catch (EOFException eofe) {
      System.out.println("End of stream");
    } catch (IOException ioe) {
      System.out.println("Input error");
    }
  }
}

					  

Assume that the file "info.dat" exits in the current directory and has only the byte values 10, 20 and 30, stored in that order.

Which statement is true about the program?

Select the one correct answer.

  1. The program will not compile because a certain unchecked exception is not caught.

  2. The program will compile and print 10|20|30|Input error.

  3. The program will compile and print 10|20|30|End of stream.

  4. The program will compile and print 10|20|30|, and then block in order to read from the file.

  5. The program will compile and print 10|20|30|, and terminate because of an uncaught exception.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint