Which statement about the following class is true? Assume the file system is available and able to be modified.
package mypkg; //from w w w . ja v a2 s.co m import java.io.File; public class Main { public boolean m(String tree) { if(new File(tree).exists()) { return true; } else { return new File(tree).mkdir(); } } public static void main(String[] seeds) { final Main creator = new Main(); System.out.print(creator.m("/p1/mypkg")); } }
C.
The class compiles, making Option E incorrect.
If /p1/mypkg exists, then the first branch of the if-then statement executes, printing true at runtime.
If /p1/mypkg does not exist, the program will print true if /p1 exists and false if /p1 does not exist.
Unlike mkdirs()
, which if used would always return true in this case, mkdir()
will return false if part of the parent path is missing.
Option C is correct, and Options A and B are incorrect.
Option D is incorrect.
This code is not expected to throw an exception at runtime.
If the path could not be created, the mkdir()
method just returns false.