Java OCA OCP Practice Question 332

Question

Given:

public class MyFileVisitor extends SimpleFileVisitor<Path> {
  // more code here
  public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
    throws IOException {
    System.out.println("File " + file);
    if ( file.getFileName().endsWith("Test.java")) {
      // CODE HERE
    }/*from ww  w  .  j  a  va 2s  .com*/
    return FileVisitResult.CONTINUE;
  }
  // more code here
}

Which code inserted at // CODE HERE

would cause the FileVisitor to stop visiting files after it sees the file Test.java?

  • A. return FileVisitResult.CONTINUE;
  • B. return FileVisitResult.END;
  • C. return FileVisitResult.SKIP_SIBLINGS;
  • D. return FileVisitResult.SKIP_SUBTREE;
  • E. return FileVisitResult.TERMINATE;
  • F. return null;


E is correct because it is the correct constant to end the FileVisitor.

Note

B is incorrect because END is not defined as a result constant.

A, C, and D are incorrect.

While they are valid constants, they do not end file visiting.

CONTINUE proceeds as if nothing special has happened.

SKIP_SUBTREE skips the subdirectory, which doesn't even make sense for a Java file.

SKIP_SIBLINGS would skip any files in the same directory.

We can't assume there aren't other directories or subdirectories.

Therefore, we have to choose the most general answer of TERMINATE.

F is incorrect because file visitor throws a NullPointerException if null is returned as the result.




PreviousNext

Related