Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

OCP Java SE8 Programmer II Study Guide - Chapter 8 - Question 10

Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is:
The following method is designed to delete a directory tree recursively. Which of the
following properties reflect the method definition? (Choose all that apply.)

Answer options:

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.

The correct answer is:

C, F. 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.

The first thing to notice is the line numbers. It can't be that method starts from the first line without class definition.
The second thing that confuses me is the answer option F. What kind of exceptions at runtime is asked? If it is RuntimeException - that's a bit strange to take it into account, but that OK. If you mean checked exceptions, than it will not compile because of the method not declaring them. Moreover, there are no method calls that cause checked exception as far as I know: isFile(), listFiles() and delete() are "silent" methods, even if some I/O Exception occurs they just return null or false.
author & internet detective
Posts: 40912
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've added the line numbers to the errata and credited you. Thanks for pointing that out.

As for Option F, the File methods can throw a SecurityException which is a RuntimeException. (You don't need to know that.) You do need to know IO methods can throw an exception, just not the name though.
    Bookmark Topic Watch Topic
  • New Topic