Java OCA OCP Practice Question 2484

Question

Select the incorrect statements for the following code:

public static void toggleFile(Path file) throws Exception {
    DosFileAttributes attr = Files.readAttributes(file,
                               DosFileAttributes.class);
    if (attr.isHidden()) {     //line1
        Files.setAttribute(file, "dos:hidden", Boolean.FALSE);    //line2
    }
    else
        Files.setAttribute(file, "dos:hidden", Boolean.TRUE);     //line3
}
a  toggleFile() changes the attribute of a file or directory 
   to  hidden if it isn't, and vice versa (when executed on a 
   Windows system)./*w  ww . jav  a 2s . c  om*/
   
b  If  "dos:hidden" is changed to  "hidden" on lines 2 and 3,  
   toggleFile() can change the "hidden" attributes of files or 
   directories on all OSs.
   
c  Replacing attr.isHidden() with attr.hidden() will not modify the code results.

d  toggleFile can throw an UnsupportedOperationException.


b, c

Note

Option (a) is a correct statement because method toggleFile() changes the attribute "hidden" of a file to true if it's false and vice versa.

Option (b) is an incorrect statement.

If "dos:hidden" is changed to "hidden", the code will throw a runtime exception.

"hidden" is a DOS file attribute.

DOS and POSIX attributes must be prefixed with dos: or posix: in method Files.setAttribute.

You don't need to prefix the basic file attributes with basic:.

In the absence of any prefix, basic: is added automatically.

Option (c) is an incorrect statement because the correct method name to access the attribute "hidden" from DosFileAttributes is isHidden.

Option (d) is a correct statement because if you execute this code on an OS that doesn't support DosFileAttributes, Files.readAttributes() will throw an UnsupportedOperationException.




PreviousNext

Related