Exam 1z0-816 All QuestionsBrowse all questions from this exam
Question 1

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?

    Correct Answer: B

    The code attempts to copy the existing file /scratch/exam/temp.txt to /scratch/exam/new.txt without checking if the destination file already exists. Since the destination file does not exist initially, this operation succeeds. However, during the next copy operation, it tries to copy /scratch/exam/new.txt to the directory /scratch/, aiming to create a file /scratch/new.txt, which also does not exist. As both these operations would be successful, the program does not throw NoSuchFileException. Ultimately, it tries to delete /scratch/exam/new.txt, which was previously copied successfully. Therefore, the program does not throw NoSuchFileException but rather completes successfully. Therefore, the correct answer is that a FileAlreadyExistsException is thrown because /scratch/exam/new.txt is attempted to be copied again without specifying the option to replace existing files.

Discussion
PontPontOption: B

Correct Answer: B

rvv007

nether of them right correct answer would be 'Compilation error' bcz of line Path outputFile = Paths get( SPACE instead of DOT