Java OCA OCP Practice Question 2588

Question

Which one of the following code snippets shows the correct usage of try-with-resources statement?

a)  public static void main(String []files) {
            try (FileReader inputFile
                = new FileReader(new File(files[0]))) {
                    //...
            }//from  w  w w  . j  ava 2  s .  c  om
            catch(IOException ioe) {}
    }

b)  public static void main(String []files) {
            try (FileReader inputFile
                = new FileReader(new File(files[0]))) {
                    //...
            }
            finally {/*...*/}
            catch(IOException ioe) {}
    }

c)  public static void main(String []files) {
            try (FileReader inputFile
                = new FileReader(new File(files[0]))) {
                    //...
            }
            catch(IOException ioe) {}
            finally {/*...*/}
    }

d)  public static void main(String []files) {
            try (FileReader inputFile
                = new FileReader(new File(files[0]))) {
                    //...
            }
    }


a)

Note

Options b) and c) uses the finally block, which is not applicable with try-with-resource statements.

In option d), the catch block is missing, which makes it wrong.




PreviousNext

Related