Java OCA OCP Practice Question 316

Question

Given:

new File("c:/temp/test.txt").delete();

How would you write this line of code using Java 7 APIs?

A.  Files.delete(Paths.get("c:/temp/test.txt"));
B.  Files.deleteIfExists(Paths.get("c:/temp/test.txt"));
C.  Files.deleteOnExit(Paths.get("c:/temp/test.txt"));
D.  Paths.get("c:/temp/test.txt").delete();
E.  Paths.get("c:/temp/test.txt").deleteIfExists();
F.  Paths.get("c:/temp/test.txt").deleteOnExit();


B is correct because, like the Java 7 code, it returns  false if the file does not exist.

Note

A is incorrect because this code throws an Exception if the file does not exist.

C, D, E, and F do not compile.

There is no deleteOnExit() method, and file operations such as delete occur using the Files class rather than the path object directly.




PreviousNext

Related