Java OCA OCP Practice Question 2589

Question

Given that:

  • Exception is the superclass of IOException and
  • IOException is the superclass of FileNotFoundException

and

2. import java.io.*;  
3. class Shape {  
4.   protected void write() throws IOException { }  
5. }  /*from   w w  w .  j ava  2 s.  c  om*/
6. public class Square extends Shape {  
7.    private void write(int x) { }  
8.    protected void write(long x) throws FileNotFoundException { }  
9.    protected void write(boolean x) throws Exception { }  
10.   protected int write(short x) { return 7; }  
11.   public void write() { }  
12. } 

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

  • A. Compilation succeeds.
  • B. Compilation fails due to an error on line 7.
  • C. Compilation fails due to an error on line 8.
  • D. Compilation fails due to an error on line 9.
  • E. Compilation fails due to an error on line 10.
  • F. Compilation fails due to an error on line 11.


A is correct.

Note

All of the overrides and overloads are legal.

B, C, D, E, and F are incorrect because it's legal for overloaded methods to have different return types, throw different exceptions, and have different access modifiers.

Overridden methods have stricter requirements, but lines 7-10 are overloaded methods.

Line 11 is an overridden method, and it's legal for overridden methods to have fewer exceptions and less restrictive access modifiers.




PreviousNext

Related