Java SE 8 Programmer II — Question 46
/green.txt and /colors/yellow.txt are accessible, and the code fragment:
Given that -
Path source = Paths.get("/green.txt);
Path target = Paths.get("/colors/yellow.txt);
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
Files.delete(source);
Which statement is true?
green.txt file content is replaced by the yellow.txt file content and the yellow.txt file is deleted.
Answer options
- A. The yellow.txt file content is replaced by the green.txt file content and an exception is thrown.
- B. The green.txt is moved to the /colors directory.
- C. The file FileAlreadyExistsException is thrown at runtime.
- D. A
Correct answer: A
Explanation
The correct answer is A because the code attempts to move green.txt to a location where yellow.txt already exists, leading to a FileAlreadyExistsException. Options B and C are incorrect as they do not accurately describe the result of the operation, and D is just a repeat of A.