This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line!
See this thread for details.
Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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: 39195
725
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!