Java OCA OCP Practice Question 2472

Question

Given the following directory structure

root dir
 |- MyDir
   |- 8_1.java
   |- 8_1.class
   |- Hello.txt

which options when inserted at /* INSERT CODE HERE */will delete the file represented by the Path object path?.

import java.nio.file.*;
class Main {
    public static void main(String... args) throws Exception {
        Path path = Paths.get("Hello.txt");
        Files.delete(/* INSERT CODE HERE */);
    }
 }
a  path.toAbsolutePath()
b  path.resolveSibling("Main.class")
c  path.toRealPath()
d  path.resolve()


a, c

Note

Option (b) is incorrect.

The call path.resolveSibling("Main.class") will resolve the path from file Main.class against the parent directory of file Hello.txt.

Because these files exist in the same directory, a (valid) path to file Main.class is returned.

So Files.delete() will not delete file Hello.txt, but Main.class instead.

Option (d) will fail compilation.

Method resolve() accepts either a Path or a String, resolving it against the path on which it's called.




PreviousNext

Related