Java Inheritance Interview MCQ Questions and Answers Part2

Study and learn Interview MCQ Questions and Answers on Inheritance in Java. You can know about Multilevel and Multiple Inheritances. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.

Go through Java Theory Notes on Inheritance before reading these objective questions.



1) What is the maximum number of levels possible in a Multilevel Inheritance in Java?
A) 8
B) 16
C) 32
D) No maximum level
Answer [=]
D
Explanation:

There is no limit to the number of levels in a multilevel inheritance chain in Java.

2) What is the output of the below java program with Constructors and Inheritance?
class Processor
{
  Processor()
  {
    System.out.print("Inside Processor() Constructor. ");
  }
}
class I3Processor extends Processor
{
  I3Processor()
  {
    System.out.print("Inside I3Processor() Constructor. ");
  }
}
class I5Processor extends I3Processor
{
  I5Processor()
  {
    System.out.print("Inside I5Processor() Constructor. ");
  }
}

public class JavaInheritance2
{
  public static void main(String[] args)
  {
    I5Processor i5 = new I5Processor();
  }
}
A) Inside I5Processor() Constructor. Inside I3Processor() Constructor. Inside Processor() Constructor. 
B) Inside I5Processor() Constructor. Inside I5Processor() Constructor. Inside I5Processor() Constructor. 
C) Inside Processor() Constructor. Inside I3Processor() Constructor. Inside I5Processor() Constructor. 
D) Compiler error
Answer [=]
C
Explanation:

The example demonstrates the Invoking of constructors using a Multilevel Inheritance. Always, the default superclass's constructor is invoked. And then, the subclass's constructor is invoked.

Processor <-- I3Processor <-- I5Processor
3) What is the output of the below Java program with Constructors using Inheritance?
class Ant
{
  Ant(String name)
  {
    System.out.print("Inside Ant(String) Constructor. ");
  }
}
class WildAnt extends Ant
{
  WildAnt()
  {
    System.out.print("Inside WildAnt() Constructor. ");
  }
}

public class Inheritance3
{
  public static void main(String[] args)
  {
    WildAnt wa = new WildAnt();
  }
}
A) Inside WildAnt() Constructor.
B) Inside Ant(String) Constructor. Inside WildAnt() Constructor.
C) Inside WildAnt() Constructor. Inside WildAnt() Constructor. 
D) Compiler error
Answer [=]
D
Explanation:

Compiler throws an error "implicit constructor of Ant class is undefined". Once you define a Constructor with some arguments, the compiler does not add a default constructor with zero arguments. To subclass a class, defining a default constructor is mandatory.

4) If a class is not subclassed by any class, then defining a default constructor is not mandatory in Java. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

True.

5) What is the output of the below Java program?
final class Bus
{
  void show()
  {
    System.out.print("Generic Bus. ");
  }
}
class ElectricBus extends Bus
{
  void show()
  {
    System.out.println("Electric Bus. ");
  }
}
public class Inheritance4
{
  public static void main(String[] args)
  {
    ElectricBus eb = new ElectricBus();
    eb.show();
  }
}
A) Generic Bus
B) Electric Bus
C) Generic Bus. Electric Bus.
D) Compiler error.
Answer [=]
D
Explanation:

Notice the use of the keyword "final" before the superclass BUS. You can not subclass a class that is marked final.

6) A Superclass reference can refer to a Subclass Object without casting. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

Yes.

7) A superclass reference can not be used to invoke a method or variable of the subclass. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

Yes. A superclass reference knows only about the methods and properties of the same class but not the subclass.



8) A subclass object can be used to invoke (call) a method or property of the superclass. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

Yes, True.

9) What is the output of the below Java program on the references of Superclass and Subclass?
class Food
{
  void show()
  {
    System.out.print("FOOD ");
  }
}
class Bread extends Food
{
  void toast()
  {
    System.out.print("TOASTED ");
  }
}
public class Inheritance5
{
  public static void main(String[] args)
  {
    Food foo = new Food();
    foo.show();
    Food foo2 = new Bread();
    foo2.show();
    Bread br = new Bread();
    br.toast();
    br.show();
  }
}
A) FOOD FOOD FOOD FOOD 
B) FOOD FOOD TOASTED FOOD 
C) FOOD TOASTED FOOD FOOD 
D) Compiler error
Answer [=]
B
Explanation:

You can only invoke methods of the Superclass using a Superclass reference variable pointing to a Subclass object.

10) What is the output of the below Java program using Inheritance?
class Furniture
{
  void show()
  {
    System.out.println("Made of Wood. ");
  }
}
class Sofa extends Furniture
{
  void addCushion()
  {
    System.out.println("Added. ");
  }
}
public class Inheritance6
{
  public static void main(String[] args)
  {
    Furniture fur = new Sofa();
    fur.addCushion();
  }
}
A) Added.
B) No output
C) Added. Made of Wood.
D) Compiler error
Answer [=]
D
Explanation:

Yes. The compiler throws an error saying "The method addCushion() is undefined for the type Furniture". It means that you can not call a method of subclass using a superclass reference even though it is pointing to a subclass object.