Java SE 11 Developer — Question 36
Given the code fragment:
Path source = Paths.get(“/repo/a/a.txt”);
Path destination = Paths.get(“/repo”);
Files.move(source, destination); // line 1
Files.delete (source);// line 2 -
Assuming the source file and destination folder exist, what Is the result?
Answer options
- A. A java.nio.file.FileAlreadyExistsException is thrown on line 1.
- B. A java.nio.file.NoSuchFileException is thrown on line 2.
- C. A copy of /repo/a/a.txt is moved to the /repo directory and /repo/a/a.txt is deleted.
- D. a.txt is renamed repo.
Correct answer: C
Explanation
The correct answer is C because the Files.move method transfers the file to the destination directory and deletes the original file. Option A is incorrect as there is no existing file conflict in this scenario, while option B is wrong because the source file is successfully moved before the delete operation. Option D is inaccurate since the file is not renamed; it is moved.