Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard

Question 86

Question ID: UKOCP29169


Given code of Test.java file:


What will be the result of compiling and executing Test class?

  • A

    ACCESS REQUIRED

    LEVEL 1

    DONE

  • B

    ACCESS REQUIRED

    LEVEL 1

  • C ACCESS REQUIRED
  • D

    ACCESS REQUIRED

    DONE

  • E Compilation error
Reveal correct answer

Correct answer: B

Explanation

UKOCP29169:

findSecretFile() method declares to throw FileNotFoundException (checked exception) and main(String []) method invokes findSecretFile(); method.

catch-handler in this case can specify FileNotFoundException or IOException or Exception or Throwable. As catch-handler for Throwable is available, hence exception is handled correctly inside main(String []) method.


As, return; statement is available inside catch-block only, therefore compiler doesn't tag System.out.println("DONE"); as unreachable.

If return; statement was present in both try and catch block or finally-block, then unreachable code compilation error would have caused by the last statement in main(String []) method.


Throwable is the root class of the exception hierarchy and it contains some useful constructors:


1. public Throwable() {...} : No-argument constructor

2. public Throwable(String message) {...} : Pass the detail message

3. public Throwable(String message, Throwable cause) {...} : Pass the detail message and the cause

4. public Throwable(Throwable cause) {...} : Pass the cause


Exception and RuntimeException classes also provide similar constructors.


Throwable class also contains methods, which are inherited by all the subclasses (Exception, RuntimeException etc.)

1. public String getMessage() {...} : Returns the detail message (E.g. detail message set by 2nd and 3rd constructor)

2. public String toString() {} :

Returns a short description of this throwable. The result is the concatenation of:

the name of the class of this object

": " (a colon and a space)

the result of invoking this object's getLocalizedMessage() method


If getLocalizedMessage returns null, then just the class name is returned.



On execution, main(String []) method invokes findSecretFile(), which throws an instance of FileNotFoundException.

There is a matching catch-handler available in main(String []) method, therefore System.out.println(ex.getMessage()); is executed and this prints ACCESS REQUIRED on to the console.

As there is finally-block available, so just before the return; statement takes control out of main(String []) method, code inside finally-block gets executed and this prints LEVEL 1 on to the console.

Control exits the main(String []) method and program terminates successfully.

Discussion

Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need