• 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

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: 41582
881
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.
 
Yeah, but is it art? What do you think tiny ad?
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic