Java OCA OCP Practice Question 3120

Question

Consider the following program:

import java.lang.*;

class Exception1 extends IllegalArgumentException {}
class Exception2 extends IllegalArgumentException {}

class BaseClass {
        void foo() throws IllegalArgumentException {
                throw new IllegalArgumentException();
        }//from  w w w  . j a va2 s  .c o  m
}

class DeriClass extends BaseClass {
        public void foo() throws IllegalArgumentException {
                throw new Exception1();
        }
}

class DeriDeriClass extends DeriClass {
        public void foo() {           // LINE A
                throw new Exception2();
        }
}
public class Main {
        public static void main(String []args) {
               try {
                       BaseClass base = new DeriDeriClass();
                       base.foo();
               } catch(RuntimeException e) {
                       System.out.println(e);
               }
        }
}

Which one of the following options correctly describes the behavior of this program?

  • a) The program prints the following: Exception2.
  • b) The program prints the following: RuntimeException.
  • c) The program prints the following: IllegalArgumentException.
  • d) The program prints the following: Exception1.
  • e) When compiled, the program will result in a compiler error in line marked with comment Line A due to missing throws clause.


a)

Note

It is not necessary to provide an Exception thrown by a method when the method is overriding a method defined with an exception (using the throws clause).

Hence, the given program will compile successfully and it will print Exception2.




PreviousNext

Related