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

Question 87

Question ID: UKOCP42123


Given code of Test.java file:


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

  • A Compilation error in both Super and Sub classes
  • B Compilation error only in Super class
  • C Compilation error only in Sub class
  • D Test class executes successfully and prints CARPE DIEM on to the console
  • E Test class executes successfully and prints DIEM CARPE on to the console
Reveal correct answer

Correct answer: D

Explanation

UKOCP42123:

It is legal for the constructors to have throws clause.

Constructors are not inherited by the Sub class so there is no method overriding rules related to the constructors but as one constructor invokes other constructors implicitly or explicitly by using this(...) or super(...), hence exception handling becomes interesting.


Java compiler adds super(); as the first statement inside Sub class's constructor:


super(); invokes the constructor of Super class (which declares to throw RuntimeException), as RuntimeException is unchecked exception, therefore no handling is necessary in the constructor of Sub class.

Sub class's constructor declares to throw IOException but main(String []) method handles it.


There is no compilation error and output is: CARPE DIEM

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