Inheritance

1.What is the primary purpose of inheritance in Java?
  • A. To allow a class to inherit methods and fields from another class
  • B. To restrict access to private variables
  • C. To execute multiple threads simultaneously
  • D. To define multiple constructors in a class

To allow a class to inherit methods and fields from another class

To allow a class to inherit methods and fields from another class

2.What happens if a subclass does not define its own constructor?
  • A. Java throws a compilation error
  • B. The superclass constructor is automatically called
  • C. The subclass is unable to create objects
  • D. Only static methods can be accessed in the subclass

The superclass constructor is automatically called

The superclass constructor is automatically called

3.What is the correct way to call a superclass method from a subclass?
  • A. this.methodName();
  • B. super.methodName();
  • C. methodName();
  • D. Parent.methodName();

super.methodName();

super.methodName();

4.Why is super() the first statement in a subclass constructor?
  • A. To initialize the subclass first
  • B. To call the superclass constructor before executing the subclass constructor
  • C. To prevent memory leaks
  • D. It is optional and can be placed anywhere in the constructor

To call the superclass constructor before executing the subclass constructor

To call the superclass constructor before executing the subclass constructor

5.In a multilevel inheritance structure, which constructor is executed first?
  • A. The subclass constructor
  • B. The superclass constructor
  • C. The most recent subclass constructor
  • D. The topmost superclass constructor

The topmost superclass constructor

The topmost superclass constructor