Java OCA OCP Practice Question 2482

Question

Which options are true for the following code?

public static void check(Path path) throws Exception {
    if (!Files.exists(path)) {
        System.out.println("Not exists");
        Files.createDirectories(path.getParent());
        Files.createFile(path);//from w  w w .  j  a  v a  2  s .c  om
    }
    else if (!Files.notExists(path)) {
        System.out.println("exists");
        Files.delete(path);
    }
    else {
        System.out.println("can never reach here");
    }
}
  • a check() will output "Not exists" and try to create a new file, if it doesn't exist.
  • b check() will output "exists" and try to delete the file if it exists. It won't delete a directory.
  • c check() can never output "can never reach here".
  • d check() can throw an IOException, NoSuchFileException.


a, d

Note

Option (b) is incorrect because Files.

delete() can be used to delete files and empty directories.

If you try to delete a nonempty directory, Files.

delete() will throw a DirectoryNotEmptyException.

Option (c) is incorrect because both exists() and notExists() can return false if the underlying system can't determine the existence of a file.




PreviousNext

Related