Java OCA OCP Practice Question 1892

Question

Which of the following statements about the deleteTree() method is correct?

public void deleteTree(File f) { 
   if(!f.isDirectory()) 
      f.delete(); 
   else { 
      Stream.of(f.list()) 
         .forEach(s -> deleteTree(s)); 
      f.deleteDirectory(); 
   } 
} 
  • A. It compiles and is capable of deleting a directory tree.
  • B. If one line were modified, it would be capable of deleting a directory tree.
  • C. If two lines were modified, it would be capable of deleting a directory tree.
  • D. None of the above


C.

Note

The code contains two compilation errors.

First, the File list() method returns a list of String values, not File values, so the call to deleteTree() with a String value does not compile.

Either the call would have to be changed to f.listFiles() or the lambda expression body would have to be updated to deleteTree(new File(s)) for the code to work properly.

Next, there is no deleteDirectory() method in the File class.

Directories are deleted with the same delete() method used for files, once they have been emptied.




PreviousNext

Related