Java OCA OCP Practice Question 499

Question

Consider these classes, defined in separate source files:

1. public class Test1 { 
2.   public float aMethod(float a, float b) 
3.          throws IOException {... 
4.   } 
5. } 

1. public class Test2 extends Test1 { 
2. 
3. } 

Which of the following methods would be legal (individually) at line 2 in class Test2?

Choose all that apply.

A.  float aMethod(float a, float b) {...} 
B.  public int aMethod(int a, int b) throws Exception {...} 
C.  public float aMethod(float a, float b) throws  Exception {...} 
D.  public float aMethod(float p, float q) {...} 


B, D.

Note

A is illegal because it is less accessible than the original method.

The fact that it throws no exceptions is perfectly acceptable.

B is legal because it overloads the method of the parent class.

It is not constrained by any rules governing its return value, accessibility, or argument list.

The exception thrown by C makes that method illegal.

D is legal because the accessibility and return type are identical, and the method is an override because the types of the arguments are identical.

The names of the arguments are irrelevant.

The absence of an exception list in D is not a problem.

An overriding method may legitimately throw fewer exceptions than its original, but it may not throw more.




PreviousNext

Related