Java OCA OCP Practice Question 1940

Question

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: } 
  • A. It does not compile because IOException is neither handled nor declared.
  • B. It does not compile for some other reason.
  • C. The method compiles and returns a Path value that is always equivalent to the input argument.
  • D. The method compiles and returns a Path value that may not be equivalent to the input argument.


C.

Note

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.




PreviousNext

Related