Java OCA OCP Practice Question 2998

Question

Given two files:

1. package com.mypkg;  
2. public class MyClass {  
3.   void do1() { System.out.print("do1 "); }  
4.   protected void do2() { System.out.print("do2 "); }  
5.   public void do3() { System.out.print("do3 "); }  
6. }  /*from   w  ww . j  a va2  s. c o m*/
 
1. import com.mypkg.*;  
2. public class UPS extends MyClass {  
3.   public static void main(String[] args) {  
4.     MyClass u = new MyClass();  
5.     u.do1();  
6.     u.do2();  
7.     u.do3();  
8. } } 

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

  • A. do1 do2 do3
  • B. "do1 ", followed by an exception.
  • C. Compilation fails due to an error on line 4 of UPS.
  • D. Compilation fails due to an error on line 5 of UPS.
  • E. Compilation fails due to an error on line 6 of UPS.
  • F. Compilation fails due to an error on line 7 of UPS.


D and E are correct

Note

Lines 5 and 6 will NOT compile.

If the protected method do2() was invoked through inheritance (for instance, "new UPS().do2();"), it would compile.




PreviousNext

Related