Java MCQs – Inheritance and polymorphism

Explore the fascinating concepts of Inheritance and Polymorphism in Java through a series of fun and informative Multiple Choice Questions (MCQs). Dive into the world of superclass and subclass relationships, discover how methods can change their behavior, and learn about the flexibility of polymorphism. These MCQs are like puzzles that help you understand these important Java ideas better. Whether you’re just starting to learn or you’ve been coding for a while, these questions will help you become a Java superstar when it comes to Inheritance and Polymorphism.

1.) What is inheritance in Java?

A) A mechanism to hide data
B) A mechanism to create new classes
C) A mechanism to achieve multiple inheritance
D) A mechanism to reuse attributes and methods of an existing class

Answer: Option D

Explanation: Inheritance allows a new class to inherit attributes and methods from an existing class, promoting code reuse and extensibility.

2.) Which keyword is used to establish inheritance in Java?

A) extend
B) inherit
C) derive
D) extends

Answer: Option D

Explanation: The “extends” keyword is used to indicate that a class is inheriting from another class (superclass).

3.) Which keyword is used to achieve method overriding in Java?

A) override
B) over
C) virtual
D) @Override

Answer: Option D

Explanation: The “@Override” annotation is used to indicate that a method is intended to override a method in the superclass.

4.) Which access modifier allows a subclass to widen the visibility of a method when overriding it?

A) private
B) protected
C) public
D) default (package-private)

Answer: Option C

Explanation: When overriding a method, the access modifier in the subclass can be the same or more accessible than the access modifier in the superclass. “public” is more accessible.

5.) What is the purpose of the “super” keyword in Java inheritance?

A) To create a new instance of the superclass
B) To access static methods
C) To call the constructor of the superclass
D) To override methods in the superclass

Answer: Option C

Explanation: The “super” keyword is used to invoke the constructor of the superclass from a subclass constructor.

6.) What is method overriding in Java?

A) Creating multiple methods with the same name but different parameters
B) Calling a method from within another method
C) Reimplementing a method in the subclass with the same signature as in the superclass
D) Defining methods inside the main method

Answer: Option C

Explanation: Method overriding involves providing a new implementation for a method in a subclass that is already defined in its superclass.

7.) What is polymorphism in Java?

A) The ability of a class to inherit multiple interfaces
B) The ability of an object to take on multiple forms through inheritance and interface implementation
C) The ability of a method to be overridden by multiple subclasses
D) The ability of a class to have multiple constructors

Answer: Option B

Explanation: Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling flexibility and dynamic behavior.

8.) Which type of polymorphism is resolved at compile-time and is based on method signatures?

A) Compile-time polymorphism
B) Run-time polymorphism
C) Method polymorphism
D) Static polymorphism

Answer: Option A

Explanation: Compile-time polymorphism is achieved through method overloading and is determined at compile time-based on method signatures.

9.) What is dynamic (run-time) polymorphism in Java?

A) The ability to create objects at runtime
B) The ability of a class to have multiple constructors
C) The ability to override methods in a subclass
D) The ability to determine the method to invoke at runtime based on the object’s actual type

Answer: Option D

Explanation: Dynamic polymorphism is achieved through method overriding, where the method to be invoked is determined at runtime based on the actual type of the object.

10.) What is a superclass in Java?

A) A class that inherits from another class
B) A class that is created after a subclass
C) A class that provides methods for polymorphism
D) A class that is lower in the class hierarchy

Answer: Option A

Explanation: A superclass is a class from which another class (subclass) inherits attributes and methods.