Java SE 11 Developer — Question 8
Given the code fragment:
Path currentFile = Paths.get(“/scratch/exam/temp.txt”);
Path outputFile = Paths get(“/scratch/exam/new.txt”);
Path directory = Paths.get(“/scratch/”);
Files.copy(currentFile, outputFile);
Files.copy(outputFile, directory);
Files.delete (outputFile);
The /scratch/exam/temp.txt file exists. The /scratch/exam/new.txt and /scratch/new.txt files do not exist.
What is the result?
Answer options
- A. /scratch/exam/new.txt and /scratch/new.txt are deleted.
- B. The program throws a FileaAlreadyExistsException.
- C. The program throws a NoSuchFileException.
- D. A copy of /scratch/exam/new.txt exists in the /scratch directory and /scratch/exam/new.txt is deleted.
Correct answer: C
Explanation
The correct answer is C because the attempt to copy the non-existing /scratch/exam/new.txt file results in a NoSuchFileException. Option A is incorrect as no files are deleted, option B is wrong since there is no existing file to trigger a FileAlreadyExistsException, and option D is incorrect because the copy operation cannot succeed if the source file does not exist.