• 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
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 39128
723
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.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!