Java MCQs – Inheritance and polymorphism

41.) In Java, what is the role of an abstract class in relation to polymorphism?

A) An abstract class restricts polymorphism to its subclasses only.
B) An abstract class eliminates the need for polymorphism.
C) An abstract class cannot be used in polymorphism.
D) An abstract class provides a base for achieving polymorphism.

Answer: Option D

Explanation: An abstract class serves as a foundation for polymorphism by allowing subclasses to provide concrete implementations.

42.) Consider an abstract class A with a final method “display().” Can a subclass B override this method?

A) Yes, by using the “override” keyword.
B) Yes, by using the “final” keyword.
C) No, the final method cannot be overridden.
D) Yes, only if subclass B is also abstract.

Answer: Option C

Explanation: A final method in a superclass cannot be overridden in any subclass.

43.) In Java, can a class be both an abstract class and an interface at the same time?

A) No, a class cannot be both an abstract class and an interface.
B) Yes, but only if the class has no methods.
C) Yes, but only if the interface is also abstract.
D) Yes, a class can be both an abstract class and an interface.

Answer: Option D

Explanation: A class can be declared as abstract and implement interfaces simultaneously.

44.) What is the purpose of the “default” method in an interface in Java 8 and later versions?

A) To mark a method as the default method of the interface
B) To provide a default implementation for the method
C) To override methods in subclasses
D) To prevent the method from being overridden

Answer: Option B

Explanation: The “default” method provides a default implementation that can be used by implementing classes, avoiding the need for all classes to implement the method.

45.) What is the significance of the “super()” statement in the constructor of an abstract class in Java?

A) It creates an instance of the superclass.
B) It calls the constructor of the immediate subclass.
C) It invokes the constructor of the superclass.
D) It prevents instantiation of the abstract class.

Answer: Option C

Explanation: The “super()” statement in a constructor is used to call the constructor of the superclass, allowing proper initialization.

46.) Which modifier can be applied to a method in an interface to prevent it from being inherited by implementing classes?

A) public
B) private
C) final
D) static

Answer: Option B

Explanation: Applying the “private” modifier to a method in an interface prevents it from being inherited by implementing classes.

47.) In Java, can an interface extend a class?

A) Yes, but only if the interface is marked as “hybrid.”
B) Yes, but only if the class is marked as “interface.”
C) Yes, an interface can extend another interface, not a class.
D) No, an interface cannot extend a class.

Answer: Option D

Explanation: In Java, an interface can only extend other interfaces, not classes.

48.) Which type of inheritance involves inheriting from more than one class in Java?

A) Single inheritance
B) Multilevel inheritance
C) Multiple inheritance
D) Hierarchical inheritance

Answer: Option C

Explanation: Multiple inheritance involves inheriting attributes and methods from more than one class.

49.) In Java, what is a covariant return type?

A) A return type that is always void.
B) A return type that is more specific than the overridden method’s return type.
C) A return type that is less specific than the overridden method’s return type.
D) A return type that is always an interface.

Answer: Option B

Explanation: A covariant return type allows a subclass method to return a more specific type than the overridden method in the superclass.

50.) Can a class be marked as both “final” and “abstract” in Java?

A) Yes, but only if it has no methods.
B) Yes, a class can be marked as both “final” and “abstract.”
C) No, a class cannot be both “final” and “abstract.”
D) Yes, but only if it is marked as “protected.”

Answer: Option C

Explanation: A class marked as “final” cannot be extended (subclassed), while an abstract class is meant to be extended. Therefore, they are contradictory modifiers.