Java OCA OCP Practice Question 3037

Question

Choose the correct option for this code snippet:

public static void main(String []files) {
    try (FileReader inputFile = new FileReader(new File(files[0]))) { // #1
            inputFile.close();                                        // #2
    }
    catch (FileNotFoundException | IOException e) {                   // #3
            e.printStackTrace();
    }
}
  • a) the code snippet will compile without any errors
  • b) the compiler will report an error at statement marked with the comment #1
  • c) the compiler will report an error at statement marked with the comment #2
  • d) the compiler will report an error at statement marked with the comment #3


d)

Note

Both of the specified exceptions belong to the same hierarchy (FileNotFoundException derives from an IOException), so you cannot specify both exceptions together in the multi-catch handler block.

It is not a compiler error to explicitly call close() method for a FileReader object inside a try-with-resources block.




PreviousNext

Related