1Z0-809: Java SE 8 Programmer II Exam Question Answer | RecDelete Method Result

RecDelete Method Result

Question

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 ()) { recDelete (aFile.getAbsolutePath ()); } else{ 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?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

A.

The given code fragment is a method named "recDelete" that accepts a directory name as a parameter and is declared to throw an IOException. The method deletes all the ".class" files in the specified directory and its subdirectories.

The method starts by getting an array of File objects representing the files and directories in the specified directory using the listFiles() method. If the returned array is not null and has a length greater than 0, the method iterates over each File object in the array using a for-each loop.

For each file in the array, if it is a directory, the method recursively calls itself, passing the absolute path of the directory as the parameter. This will traverse the subdirectories of the current directory until all subdirectories have been processed.

If the file is not a directory, the method checks if its name ends with ".class". If it does, the file is deleted using the delete() method.

Therefore, when the method is called with the "Projects" directory as the parameter, it will delete all the ".class" files in the "Projects" directory and its subdirectories.

So, the correct answer is option A: The method deletes all the .class files in the Projects directory and its subdirectories.