Java SE 8 Programmer II — Question 108

Given the code fragment:
public void recDelete (String dirName) throws IOException {
File [ ] listOfFiles = new File (dirName) .listFiles();
if (listOfFiles ! = null && listOfFiles.length >0) {
for (File aFile : listOfFiles) {
if (!aFile.isDirectory ()) {
if (aFile.getName ().endsWith (".class"))
aFile.delete ();
}
}
}
}
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?

Answer options

Correct answer: B

Explanation

The correct answer is B because the code only deletes .class files in the specified directory, not in its subdirectories. The method does not traverse into subdirectories, hence it won't affect any .class files located within them. Options A and C are incorrect as they misinterpret the method's scope, while option D is wrong because no IOException is thrown in the provided code.