Inheritance Self Test With Answers


1) Which of the following statements is not correct. In java inheritance

a) a class can inherit from multiple parent classes
b) private variables are not accessible to a child class
c) extends is the keyword used by a subclass to extend a parent
d) inheritance provides an easy way to build on earlier work.            ( a )
 

2) Inheritance assists development in the following ways.
    (Pick the incorrect statement.)

a) improves eases of building on existing work
b) provides protection from entering bugs into tested code
c) permits maintenance of version compatibility
d) Provides access to all inherited code and features.                       ( d )
 

3) Masked parent variables values can be revealed by the following.
    Pick the incorrect statement.

     a) casting a child variable to the parent type
     b) using super to reference the parent object
     c) declaring an object to the parent type
     d) calling toParent( ) passing in the hidden variable                      ( d )
 

4) Consider the following classes.

 class  Water{  /* H20 */ }
class  Mist  extends Water{/* air jet over water */}
class  Steam extends Water{ /* boiling water  */ }

class TestWater
      {
      public static void main(String[]args)
      {
      Water water;
      Mist mist=new Mist( );
      Steam steam = new Steam( );
      water = new Water( );
      steam = (Steam)water;
      }
      }

This code              
             a ) won't compile
             b ) will compile and run
            c ) will compile but won't run
            d ) will compile and may run             ( c )      

// Comment: Here the parent type is cast to the child so it will compile. It won't
// run however as the actual object being assigned is the parent, Water type.
 

5) Which of the following is not true? With respect for overriding,

a)  Overriding is related to inheritance
b)  Overridden methods may differ in return type.
c)  Overridden methods share the same signature
d)  Overridden methods may throw different Exceptions             ( b)

//Comment: Regarding d, the Exceptions may be different but must be related in hierarchy
// and exceptions thrown in the overriding method must be the same or less general than the
// exception thrown in the parent.

 

6 ) Which of the following factors is only significant to one of overloading and overriding?

a) type
b) order
c) number
d) return type            ( d )

7)

class X{
   String t="XX";
   String getT( ){
   String o = "XY";
   return o;
   }
 }
class Y extends X{
   String t="YY";
   String getT( ){
   String o="YX";
   return o;
   }
 }

class XY {
  public static void main(String[]args){
   X xy = new Y( );
   System.out.println( xy.t + " " + xy.getT( ) );
   }
}

a ) XX XY
b)  YY YX
c)  XX YX
d)  YY XY                                          (  c )       
 

8) Select which two of the following methods the compile will not be able to distinguish

a) private void measure(int i){ }
b) void measure(int i, int j){ }
c) protected  void measure(int i, short j){ }
d) public void measure(short j){ }
e) void measure(int k, int x) { }                 ( b & e )