Java OCA OCP Practice Question 1367

Question

Given the following classes and declarations,

which of these statements about // 1 and //2 are true?

class MyBaseClass { 
   private int i = 10; 
   public void  f (){} 
   public void g (){} 
} 

class MyClass extends MyBaseClass { 
   public int i = 20; 
   public void g (){} 
} 

public class C{ 
   MyBaseClass a = new MyBaseClass ();//1 
   MyBaseClass b = new MyClass ();//2 
} 

Select 1 option

  • A. System .out.println (b .i); will print 10.
  • B. The statement b .f ( ); will give compile time error..
  • C. System .out.println (b .i); will print 20
  • D. All the above are correct.
  • E. None of the above statements is correct.


Correct Option is  : E

Note

For Option A.

Since variable b is declared as of class MyBaseClass, you cannot do b.i even if the actual object is of class MyClass because i in MyBaseClass is private.

For Option B.

class MyBaseClass has f () so b .f () is legal.

For Option C.

Since variable b is declared as of class MyBaseClass, you cannot do b.i even if the actual object is of class MyClass because i in MyBaseClass is private.




PreviousNext

Related