Java OCA OCP Practice Question 2868

Question

The following method is designed to delete a directory tree recursively.

Which of the following properties reflect the method definition? (Choose all that apply.)

1: public static void deleteTree(File file) { 
2:    if(!file.isFile()) 
3:       for(File entry: file.listFiles()) 
4:          deleteTree(entry); 
5:    else file.delete(); 
6: } 
  • A. It can delete a directory that contains only files.
  • B. It can delete a directory tree of arbitrary length.
  • C. It can delete a single file.
  • D. The code will not compile because of line 2.
  • E. The code will not compile because of line 3.
  • F. It compiles but may throw an exception at runtime.


C, F.

Note

The code compiles, so D and E are incorrect.

There is a bug in the method in that file.

delete() should be executed at the end of the method for both files and directories alike.

As written, the method will delete all files within a directory but none of the directories themselves.

Therefore, A and B are incorrect and C is correct.

F is correct, because most methods in the File class that interact with the file system are capable of throwing an exception at runtime, such as when the directory does not exist.




PreviousNext

Related