Which statement about the following Main class is correct?
1: package mypkg; 2: import java.nio.file.*; 3: public class Main { 4: public Path makeAbsolute(Path p) { 5: if(p!=null && !p.isAbsolute()) 6: return p.toAbsolutePath(); 7: return p; 8: } 9: }
C.
The code compiles without issue, so Options A and B are incorrect.
While many of the Files methods do throw IOException, most of the Path methods do not throw a checked exception.
The lack of indent of the return statement on line 6 is intentional and does not prevent the class from compiling.
If the input argument p is null or not an absolute path, then the if-then clause is skipped, and it is returned to the caller unchanged.
If the input argument is an absolute path, then calling toAbsolutePath()
has no effect.
In both cases, the return value of the method matches the input argument, making Option C the correct answer.