Java OCA OCP Practice Question 230

Question

Given:

3. class SubException extends Exception { }
4. class SubSubException extends SubException { }
5.
6. public class Main { void doStuff() throws SubException { } }
7.
8. class Main2 extends Main { void doStuff() throws SubSubException { } }
9.
10. class Main3 extends Main { void doStuff() throws Exception { } }
11./*from w  ww.  java 2  s.co  m*/
12. class Main4 extends Main { void doStuff(int x) throws Exception { } }
13.
14. class Main5 extends Main {  void doStuff()  { } }

What is the result? (Choose all that apply.)

  • A. Compilation succeeds
  • B. Compilation fails due to an error on line 8
  • C. Compilation fails due to an error on line 10
  • D. Compilation fails due to an error on line 12
  • E. Compilation fails due to an error on line 14


C is correct.

Note

An overriding method cannot throw a broader exception than the method it's overriding.

Class Main4's method is an overload, not an override.




PreviousNext

Related