Java OCA OCP Practice Question 2962

Question

What is the result of executing the following code? (Choose all that apply.)

1:  import java.io.IOException; 
2:  import java.nio.file.*; 
3:  import java.nio.file.attribute.*; 
4:  public class Main extends SimpleFileVisitor<Path> { 
5:     private String extension; 
6:     public Main(String extension) { 
7:        this.extension = extension; 
8:     } /*from  www.j  av a 2s.c  o  m*/
9: 
10:    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) 
11:                                                 throws IOException { 
12:       if(file.getFileName().endsWith(extension) && !attrs.isSymbolicLink()) 
13:          Files.delete(file); 
14:             return FileVisitResult.CONTINUE; 
15:       } 
16: 
17:    public static void main(String[] args) throws IOException { 
18:       Files.walkFileTree(Paths.get("/p1/data"), 
19:          new Main("txt")); 
20:    } 
21: } 
  • A. It compiles and is capable of deleting all matching files in the /p1/data directory only.
  • B. It compiles and is capable of recursively deleting all matching files in the /p1/data directory tree.
  • C. The code will not compile because of lines 10-11.
  • D. The code will not compile because of line 13.
  • E. The code will not compile because of lines 19-20.
  • F. It compiles but may throw an exception at runtime.


B,F.

Note

The code compiles without issue, so C, D, and E are each incorrect.

Because a subclass of FileVisitor was used and not the DirectoryStream class, the code will recursively delete all matching files in the /p1/data directory, so B is correct and A is incorrect.

Finally, if the directory path does not exist or is not accessible in the file system, an exception will be thrown at runtime, so F is also correct.




PreviousNext

Related