Java OCA OCP Practice Question 2915

Question

Given:

3. class School {  
4.   School getDeptName() { return new School(); }  
5. }  
6. class Campus extends School {  
7.   Campus getDeptName() { return new Campus(); }  
8.   // insert code here  
10. } 

And the following four code fragments:

I.    String getDeptName(int x) { return "mktg"; } 

II.   void getDeptName(School d) { ; } 

III.  void getDeptName(long x) throws NullPointerException {  
        throw new NullPointerException();  
      } /*from  ww w. j a  v  a 2 s . c o m*/

IV.  School getDeptName() throws NullPointerException {  
     throw new NullPointerException();  
     return new School();  
     } 

Which are true? (Choose all that apply.)

  • A. If fragment I is inserted at line 8, the code compiles.
  • B. If fragment II is inserted at line 8, the code compiles.
  • C. If fragment III is inserted at line 8, the code compiles.
  • D. If fragment IV is inserted at line 8, the code compiles.
  • E. If none of the fragments are inserted at line 8, the code compiles.


A, B, C, and E are correct.

Note

As it stands, the code demonstrates a legal covariant return.

Fragments I, II, and III are legal overloads.

D is incorrect because fragment IV does not correctly overload the Campus.getDeptName() method.




PreviousNext

Related