Java OCA OCP Practice Question 2938

Question

What is the result of applying the Files.walkFileTree() method to the current directory and an instance of the following FileVisitor class?

1: public class FilePrinter implements FileVisitor<Path> { 
2:    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) 
3:                                                   throws IOException { 
4:       System.out.println("Found file: "+file.getFileName()); 
5:       return FileVisitResult.CONTINUE; 
6:    } 
7: } 
  • A. It prints out all of the filenames in the current directory.
  • B. It recursively prints out all of the filenames in the directory tree.
  • C. It recursively prints out all file and directory names in the directory tree.
  • D. The code will not compile because of line 1.
  • E. The code will not compile because of line 5.
  • F. The code compiles but it throws an exception at runtime.


D.

Note

The code does not compile because in order to implement the FileVisitor interface, all four visitor methods must be provided, so D is correct.

If instead of implementing the FileVisitor interface, it extended the SimpleFileVisitor class, which does not require any methods to be overridden, then the code would compile and produce the list of file names in the directory tree, making B the correct answer.




PreviousNext

Related