Java OCA OCP Practice Question 2952

Question

What are the possible results of executing the following code? (Choose all that apply.)

1: Path path = Paths.get("data.txt"); 
2: try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path)) { 
3:    for (Path entry: directoryStream) 
4:       System.out.println(entry.getFileName()); 
5: } 
  • A. It compiles and runs without issue.
  • B. The code will not compile because of line 2.
  • C. The code will not compile because of line 3.
  • D. It compiles but throws an InterruptedException at runtime.
  • E. It compiles but throws an IOException at runtime.


A,E.

Note

First off, the code compiles without issue, so B and C are incorrect.

Next, it is possible for a directory to have an extension in the name, so A is correct, albeit uncommon.

D is an incorrect answer, and it relates to threading, not a DirectoryStream walk process.

E is correct, at least tangentially, because NotDirectoryException and NoSuchFileException extend IOException.




PreviousNext

Related