Java OCA OCP Practice Question 1910

Question

Which of the following can be filled into the blank that would allow the method to compile?

public String getPathName(String fileName) { 
   final Path p =                                       ; 
   return p.getFileName(); 
} 
I.  new File(fileName).toPath()
II.  new Path(fileName)
III.  FileSystems.getDefault().getPath(fileName)
  • A. I and II
  • B. I and III
  • C. II
  • D. None of the above


D.

Note

The Path method getFileName() returns a Path instance, not a String.

The code does not compile, regardless of which line of code is inserted into the blank, making Option D the correct answer.

Statements I and III are two valid ways to create a Path instance.

If the method was updated to use Path as the return type, then Option B would be the correct answer.

Statement II would cause the method to not compile, because Path is an interface and requires a class to be instantiated.




PreviousNext

Related