Java OCA OCP Practice Question 633

Question

What is the output of the following application?

package mypkg; /*from  w w  w  . j  av  a2  s. c o m*/
class Pet { 
   void roar(int level) throw RuntimeException {  // m1 
      if(level<3) throw new IllegalArgumentException("Incomplete"); 
      System.out.print("Roar!"); 
   } 
} 
public class Lion extends Pet { 
   public void roar() {  // m2 
      System.out.print("Roar!!!"); 
   } 
? 
   public static void main(String[] cubs) { 
      final Pet kitty = new Lion();  // m3 
      kitty.roar(2); 
   } 
} 
  • A. The code does not compile because of line m1.
  • B. The code does not compile because of line m2.
  • C. The code does not compile because of line m3.
  • D. The code compiles but a stack trace is printed at runtime.


A.

Note

The application does not compile because the roar() method in the Pet class uses throw instead of throws, making Option A the correct answer.

If the correct keyword was used, the code would compile without issues, and Option D would be correct.

The override of roar() in the Lion class is valid, since the overridden method has a broader access modifier and does not declare any new or broader checked exceptions.




PreviousNext

Related