Java OCA OCP Practice Question 351

Question

Given a partial directory tree at the root of the drive:

dir x - |
..........| = file a.txt
..........| - dir y
....................| - file b.txt
....................| - dir y
..............................| - file c.txt

And the following snippet:

Path dir = Paths.get("c:/x");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "**/*.txt")) {
    for (Path path : stream) {
       System.out.println(path);
    } 
}

What is the result?

A.  c:/x/a.txt
B.  c:/x/a.txt
    c:/x/y/b.txt
    c:/x/y/z/c.txt
C.  Code compiles but does not output anything
D.  Does not compile because DirectoryStream comes from FileSystems, not Files
E.  Does not compile for another reason


C is correct because DirectoryStream only looks at files in the immediate directory.

Note

/*.txt means zero or more directories followed by a slash, followed by zero or more characters followed by .txt.

Since the slash is in there, it is required to match, which makes it mean one or more directories.

However, this is impossible because DirectoryStream only looks at one directory.

If the expression were simply *.txt, answer A would be correct.




PreviousNext

Related