1Z0-809 · Question #61
Assume that Projects contains subdirectories that contain .class files and is passed as an argument to the recDelete () method when it is invoked. What is the result?
The correct answer is B. The method deletes the .class files of the Projects directory only. Option B is correct because the recDelete() method attempts to delete subdirectories using File.delete(), which silently fails (returns false without throwing an exception) when a directory is non-empty. Since the subdirectories still contain .class files, they cannot be…
Question
Options
- AThe method deletes all the .class files in the Projects directory and its subdirectories.
- BThe method deletes the .class files of the Projects directory only.
- CThe method executes and does not make any changes to the Projects directory.
- DThe method throws an IOException.
How the community answered
(24 responses)- A4% (1)
- B79% (19)
- C13% (3)
- D4% (1)
Explanation
Option B is correct because the recDelete() method attempts to delete subdirectories using File.delete(), which silently fails (returns false without throwing an exception) when a directory is non-empty. Since the subdirectories still contain .class files, they cannot be deleted, and the method never recurses into them - so only the .class files directly inside Projects are removed.
Option A is wrong because true recursive deletion requires explicitly iterating into each subdirectory before trying to delete it; this method doesn't do that correctly. Option C is wrong because the method does make changes - it successfully deletes .class files at the top level of Projects. Option D is wrong because File.delete() does not throw an IOException on failure; it simply returns false, which is the classic Java File API trap.
Memory tip: Think of File.delete() as a "polite no" - it refuses to delete non-empty directories but never complains about it. If you want recursive deletion in Java, you must empty the directory first or use Files.walkFileTree().
Community Discussion
No community discussion yet for this question.