Java OCA OCP Practice Question 609

Question

Which statement about the following classes is correct?

class Exception2 extends Exception {} 

class Printable { 
   public int getSymbol() throws Exception2 { return -1; }  // g1 
} 

public class Main extends Printable { 
   public int getSymbol() { return 8; }  // g2 
   public void printData() { 
      try { //from   ww w . ja  v a  2  s. c  o  m
         System.out.print(getSymbol()); 
      } catch {  // g3 
         System.out.print("Unable to read data"); 
      } 
   } 
} 
  • A. The code does not compile because of line g1.
  • B. The code does not compile because of line g2.
  • C. The code does not compile because of line g3.
  • D. None of the above


C.

Note

The code does not compile because the catch block is missing a variable type and name, such as catch (Exception e).

Option C is the correct answer.

Both implementations of getSymbol() compile without issue, including the overridden method.

A subclass can swallow a checked exception for a method declared in a parent class; it just cannot declare any new or broader checked exceptions.




PreviousNext

Related