Java OCA OCP Practice Question 249

Question

Given:

 2. class MyException extends Exception { }
 3. class Action {
 4.   void doStuff() {  }
 5. }/*w w w  .  jav a 2  s  . c  o m*/
 6. public class Main extends Action {
 7.   public static void main(String[] args) {
 8.     new Main().doStuff();
 9.   }
10.   // insert code here
11.      System.out.println(7/0);
12.   }
13. }

And given the following four code fragments:

I.   void doStuff() {
II.  void doStuff() throws MyException {
III. void doStuff() throws RuntimeException {
IV.  void doStuff() throws ArithmeticException {

When fragments I-IV are added, independently, at line 10, which are true?

Choose all that apply.

  • A. None will compile
  • B. They will all compile
  • C. Some, but not all, will compile
  • D. All of those that compile will throw an exception at runtime
  • E. None of those that compile will throw an exception at runtime
  • F. Only some of those that compile will throw an exception at runtime


C and D are correct.

Note

An overriding method cannot throw checked exceptions that are broader than those thrown by the overridden method.

An overriding method can throw RuntimeException not thrown by the overridden method.




PreviousNext

Related