Java OCA OCP Practice Question 2923

Question

Given:

public class Main extends MyClass {
   public static void main(String[] args) {
      try {//from   w  w  w  . jav a 2  s . c  o  m
         new Main().do1();
         new Main().do2();
         new Main().do3();
      } catch (Throwable t) {
         System.out.print("exc ");
      }
   }
}

class MyClass {
   void do1() {
      System.out.print("do1 ");
   }

   private void do2() {
      System.out.print("do2 ");
   }

   protected void do3() {
      System.out.print("do3 ");
   }
}

What is the result?

  • A. do1 exc
  • B. do1 do2 exc
  • C. do1 do2 do3
  • D. Compilation fails.
  • E. An exception is thrown at runtime.


D is correct.

Note

Even a subclass can't access a superclass's private method.




PreviousNext

Related