Java MCQs – Inheritance and polymorphism

21.) What is the diamond problem in the context of multiple inheritance in Java?

A) A collision between the names of instance variables
B) A conflict arising when multiple classes inherit from a common superclass
C) A situation where a method is overridden in multiple subclasses
D) A scenario where a class has two or more ambiguous paths to a common ancestor

Answer: Option D

Explanation: The diamond problem occurs in languages with multiple inheritance when a class inherits from two or more classes that share a common ancestor.

22.) What is the purpose of the “super()” statement in a subclass constructor?

A) To call the constructor of the superclass
B) To call a static method in the superclass
C) To initialize the subclass’s attributes
D) To create a new instance of the superclass

Answer: Option A

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

23.) In Java, can you have a “final” method in a superclass that is overridden in a subclass?

A) Yes, and it can be further overridden in another subclass.
B) Yes, but it cannot be overridden in a subclass.
C) No, a “final” method cannot be overridden in any subclass.
D) Yes, but only if the subclass is in a different package.

Answer: Option B

Explanation: A “final” method in a superclass cannot be overridden in any subclass; it is a terminal point for method inheritance.

24.) Which type of class relationship represents “has-a” relationship between classes?

A) Inheritance
B) Composition
C) Polymorphism
D) Aggregation

Answer: Option B

Explanation: Composition represents a “has-a” relationship, where a class contains another class as a part.

25.) What is the concept of method hiding in Java?

A) The process of encapsulating methods in a class
B) The process of dynamically selecting methods at runtime
C) The process of overriding a method in a subclass
D) The process of defining a static method with the same name in a subclass

Answer: Option D

Explanation: Method hiding occurs when a subclass defines a static method with the same name as a static method in the superclass.

26.) Can a subclass constructor invoke more than one constructor of its superclass in Java?

A) No, a subclass constructor can only invoke one constructor of its superclass.
B) Yes, a subclass constructor can invoke any number of constructors of its superclass.
C) Yes, but only if the superclass has a default constructor.
D) Yes, but only if the superclass is abstract.

Answer: Option A

Explanation: A subclass constructor can invoke only one constructor of its superclass using the “super()” statement.

27.) Which type of polymorphism is achieved through interfaces in Java?

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

Answer: Option B

Explanation: Run-time polymorphism is achieved through method overriding and interface implementation, where the method to be invoked is determined at runtime.

28.) In Java, can a class be both abstract and final?

A) Yes, an abstract class can also be marked as final.
B) No, an abstract class cannot be marked as final.
C) Yes, but only if the class has no methods.
D) Yes, but only if the class has no attributes.

Answer: Option B

Explanation: An abstract class is meant to be extended, and marking it as final would prevent inheritance.

29.) What is the role of the “instanceof” operator in polymorphism?

A) To check if a class can be instantiated
B) To create a new instance of a class
C) To compare two objects for equality
D) To test if an object is of a specific type or interface

Answer: Option D

Explanation: The “instanceof” operator is used to determine if an object is an instance of a particular class or implements a specific interface.

30.) What is the purpose of the “protected” access modifier in Java inheritance?

A) To make a method visible only within the class
B) To restrict method access to the same package
C) To allow access only to subclasses, regardless of package
D) To make a method globally accessible

Answer: Option C

Explanation: The “protected” access modifier allows access to the member within the same package and by subclasses, regardless of whether they are in the same or different packages.