Java MCQs – Inheritance and polymorphism

31.) In Java, can an abstract class have instance variables?

A) No, abstract classes cannot have instance variables.
B) Yes, but only if the instance variables are declared as “static.”
C) Yes, abstract classes can have both instance and static variables.
D) Yes, but only if the abstract class has no methods.

Answer: Option C

Explanation: Abstract classes can have instance variables, which are inherited by subclasses.

32.) Which type of inheritance is implemented by interfaces in Java?

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

Answer: Option B

Explanation: Interfaces in Java allow a class to implement multiple interfaces, achieving a form of multiple inheritance.

33.) What is the “final” keyword used for in the context of classes and methods in Java?

A) To indicate that a class cannot be instantiated
B) To indicate that a method cannot be overridden
C) To make a method accessible only within the class
D) To specify the order of method invocation

Answer: Option B

Explanation: Marking a method as “final” prevents it from being overridden by subclasses.

34.) In Java, can an abstract class have a constructor?

A) No, abstract classes cannot have constructors.
B) Yes, but only if the constructor is private.
C) Yes, abstract classes can have both default and parameterized constructors.
D) Yes, but only if the abstract class has no attributes.

Answer: Option C

Explanation: Abstract classes can have constructors that are called when a subclass object is instantiated.

35.) Can an abstract class implement an interface in Java?

A) No, abstract classes cannot implement interfaces.
B) Yes, but only if the interface is also abstract.
C) Yes, abstract classes can implement interfaces regardless of whether the interface is abstract or not.
D) Yes, but only if the interface has a single method.

Answer: Option C

Explanation: Abstract classes can implement interfaces, providing a combination of abstract and interface-based behavior.

36.) What is dynamic method dispatch in Java polymorphism?

A) The process of calling static methods at runtime
B) The process of selecting the appropriate method based on the object’s type at runtime
C) The process of method overloading
D) The process of method hiding

Answer: Option B

Explanation: Dynamic method dispatch allows the selection of the correct method based on the actual object’s type during runtime.

37.) What is the purpose of the “superclass reference variable” in Java polymorphism?

A) To reference a subclass object from a superclass reference
B) To reference a superclass object from a subclass reference
C) To store the superclass’s attributes in a subclass
D) To access private members of the superclass

Answer: Option A

Explanation: Polymorphism allows a superclass reference variable to hold an object of a subclass, enabling dynamic method invocation.

38.) In Java, can a method be both “final” and “static”?

A) Yes, it is a common practice.
B) No, it is not allowed.
C) Yes, but only if the method is private.
D) Yes, but only if the method is public.

Answer: Option A

Explanation: A method can be both “final” and “static.” “Final” prevents overriding, and “static” indicates a class-level method.

39.) What is the purpose of the “abstract” keyword in method declaration within an interface in Java?

A) To indicate that the method can have different implementations
B) To indicate that the method is private and cannot be accessed
C) To indicate that the method has a default implementation
D) To indicate that the method has no implementation and must be overridden

Answer: Option D

Explanation: An abstract method within an interface has no implementation and must be overridden by classes that implement the interface.

40.) What is method resolution order (MRO) in Java multiple inheritance?

A) The order in which methods are declared in a class
B) The order in which methods are invoked by the compiler
C) The order in which methods are overridden in subclasses
D) The order in which methods are searched and selected during inheritance

Answer: Option D

Explanation: Method resolution order (MRO) defines the sequence in which methods are searched and selected when multiple inheritance is involved.