Java OCA OCP Practice Question 318

Question

Given:

public void read(Path dir) throws IOException {
   // CODE HERE
   System.out.println(attr.creationTime());
}

Which code inserted at // CODE HERE will compile and run without error on Windows?

Choose all that apply.

A.   BasicFileAttributes attr = Files.readAttributes(dir, BasicFileAttributes.class);
B.   BasicFileAttributes attr = Files.readAttributes(dir, DosFileAttributes.class);
C.   DosFileAttributes attr = Files.readAttributes(dir, BasicFileAttributes.class);
D.   DosFileAttributes attr = Files.readAttributes(dir, DosFileAttributes.class);
E.   PosixFileAttributes attr = Files.readAttributes(dir, PosixFileAttributes.class);
F.   BasicFileAttributes attr = new BasicFileAttributes(dir);
G.   BasicFileAttributes attr =dir.getBasicFileAttributes();


A, B, and D are correct.

Note

Creation time is a basic attribute, which means you can read BasicFileAttributes or any of its subclasses to read it.

DosFileAttributes is one such subclass.

C is incorrect because you cannot cast a more general type to a more specific type.

E is incorrect because this example specifies it is being run on Windows.

While it would work on UNIX, it throws an UnsupportedOperationException on Windows due to requesting the WindowsFileSystemProvider to get a POSIX class.

F and G are incorrect because those methods do not exist.

You must use the Files class to get the attributes.




PreviousNext

Related