Win a copy of High Performance Python for Data Analytics this week in the Python 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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: 40400
821
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.
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic