Java Inheritance MCQ

Inheritance is one of the core concepts of Object-Oriented Programming (OOP) and is foundational to Java. It allows developers to create a new class that is based on an existing class, inheriting its attributes and methods. If you're keen to test your knowledge or learn more about inheritance in Java, this quiz is for you!

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. Which keyword is used to implement inheritance in Java?

a) imports
b) extends
c) inherits
d) uses

Answer:

b) extends

Explanation:

In Java, the extends keyword is used to declare inheritance, allowing a subclass to inherit properties and methods from a superclass.

2. What is a superclass (or parent class)?

a) The class that inherits from another class
b) The class from which another class inherits
c) An interface in Java
d) A final class in Java

Answer:

b) The class from which another class inherits

Explanation:

A superclass (or parent class) provides the basis from which subclasses can inherit properties and methods.

3. Which of the following concepts allows Java classes to inherit methods and properties from multiple classes?

a) Multiple Inheritance
b) Single Inheritance
c) Polymorphism
d) Encapsulation

Answer:

a) Multiple Inheritance

Explanation:

Multiple Inheritance allows a class to inherit properties and methods from multiple classes. However, Java doesn't support multiple inheritance with classes but achieves it through interfaces.

4. Which keyword in Java is used to access the superclass's members (methods/variables)?

a) super
b) this
c) extends
d) parent

Answer:

a) super

Explanation:

The super keyword in Java is used to refer to the immediate parent class instance variable or method.

5. Which method is called automatically when an object is created?

a) init
b) main
c) new
d) constructor

Answer:

d) constructor

Explanation:

A constructor is a special method that gets called automatically when an object is instantiated. It typically initializes the newly created object.

6. In Java, if a class does not have any constructor, what will Java do?

a) Display an error
b) Provide a default constructor
c) Create an empty class
d) None of the above

Answer:

b) Provide a default constructor

Explanation:

If a class doesn't have a constructor, Java automatically provides a default no-argument constructor.

7. What will happen if a subclass provides a constructor explicitly and does not invoke the parent class constructor?

a) The program will fail to compile
b) The parent class constructor is called automatically
c) The subclass constructor will override the parent constructor
d) None of the above

Answer:

b) The parent class constructor is called automatically

Explanation:

Even if the subclass provides its constructor and doesn't explicitly invoke the superclass's constructor, Java automatically calls the no-argument constructor of the superclass.

8. Which of these can be inherited from a superclass?

a) Private methods
b) Public methods
c) Both private and public methods
d) Only constructors

Answer:

b) Public methods

Explanation:

Only public and protected members (methods and variables) of a superclass are inherited by a subclass. Private members are not inherited.

9. What is the purpose of inheritance in Java?

a) To improve security
b) To enhance the readability of the code
c) To provide a clear interface
d) To promote code reusability

Answer:

d) To promote code reusability

Explanation:

The primary purpose of inheritance is to promote code reusability. By allowing one class to inherit properties and methods from another, developers can avoid redundant code.

10. Which of the following is false about inheritance in Java?

a) A subclass can be a superclass for another class
b) Inheritance supports the concept of hierarchical classification
c) A class can only inherit from one superclass
d) A subclass can override all the superclass methods

Answer:

d) A subclass can override all the superclass methods

Explanation:

While a subclass can override many superclass methods, it cannot override private superclass methods because private methods are not accessible in the subclass.

Inheritance plays a pivotal role in Java and OOP, driving efficient and organized code. Quizzes like these can bolster your understanding of core concepts. Whether you're just beginning your Java journey or brushing up on the basics, continuous learning is key. Happy coding!



Comments