Java Inheritance MCQ Questions and Answers

1. What is inheritance in Java?

a) Copying methods from one class to another
b) The process where one class acquires the properties of another
c) The ability to run methods concurrently
d) The process of encapsulating data

Answer:

b) The process where one class acquires the properties of another

Explanation:

Inheritance in Java is a mechanism where one class inherits the attributes and methods of another class.

2. Which keyword is used for inheritance in Java?

a) super
b) extends
c) implements
d) inherit

Answer:

b) extends

Explanation:

The 'extends' keyword is used in Java to indicate that a class is inheriting from a superclass.

3. Can a subclass in Java inherit private members of its superclass?

a) Yes, always
b) No, never
c) Only if the subclass is in the same package
d) Only through getter and setter methods

Answer:

b) No, never

Explanation:

A subclass in Java cannot inherit private members (methods or variables) of its superclass.

4. What is a superclass in Java?

a) A class that inherits from another class
b) The highest class in the inheritance hierarchy
c) A class that is extended by another class
d) A class that implements an interface

Answer:

c) A class that is extended by another class

Explanation:

A superclass is a class that is extended by another class, known as the subclass.

5. What is method overriding in Java?

a) Changing the method's return type in the subclass
b) Creating a new method in the subclass
c) Providing a new implementation for an inherited method in the subclass
d) Calling a superclass method from the subclass

Answer:

c) Providing a new implementation for an inherited method in the subclass

Explanation:

Method overriding occurs when a subclass provides its own implementation of a method already defined in its superclass.

6. What does the 'super' keyword do in Java?

a) Refers to the superclass object
b) Creates an instance of the superclass
c) Deletes an instance of the superclass
d) Overloads a method in the superclass

Answer:

a) Refers to the superclass object

Explanation:

The 'super' keyword is used to refer to the immediate parent class object.

7. Can a class in Java extend multiple classes?

a) Yes, always
b) No, Java supports only single inheritance
c) Yes, but only abstract classes
d) Yes, using the 'implements' keyword

Answer:

b) No, Java supports only single inheritance

Explanation:

Java does not support multiple inheritance with classes. A class can extend only one class, but it can implement multiple interfaces.

8. What is the purpose of inheritance in Java?

a) To improve code readability
b) To increase the execution speed of the program
c) To provide data encapsulation
d) To promote code reuse and enhance maintainability

Answer:

d) To promote code reuse and enhance maintainability

Explanation:

Inheritance is used in Java to promote code reuse, reduce redundancy, and enhance the maintainability of the code.

9. What is a subclass in Java?

a) A class that inherits from a superclass
b) A class that is only used for subclassing
c) A class that cannot be instantiated
d) A class that does not inherit from any other class

Answer:

a) A class that inherits from a superclass

Explanation:

A subclass is a class that extends (inherits from) another class, known as the superclass.

10. Can a constructor be inherited in Java?

a) Yes
b) No
c) Only if it's public
d) Only if it's a default constructor

Answer:

b) No

Explanation:

Constructors are not inherited in Java. Each class has its own constructors.

11. What is an 'IS-A' relationship in Java?

a) A relationship where a class is a type of another class
b) A relationship where a class contains an instance of another class
c) A relationship between methods of different classes
d) A relationship where two classes share the same methods

Answer:

a) A relationship where a class is a type of another class

Explanation:

An 'IS-A' relationship in Java is an inheritance relationship where a subclass is a type of its superclass.

12. How does inheritance support polymorphism in Java?

a) By allowing different classes to have methods with the same name
b) By enabling a subclass to have multiple forms
c) By allowing a subclass to define its own unique behavior
d) Both a and c

Answer:

d) Both a and c

Explanation:

Inheritance supports polymorphism by allowing subclasses to define their own unique behaviors and by enabling methods in different classes to have the same name but different implementations.


Comments