Java MCQs – Inheritance and polymorphism

11.) What is a subclass in Java?

A) A class that is lower in the class hierarchy
B) A class that inherits from another class (superclass)
C) A class that overrides methods in the superclass
D) A class that is created before a superclass

Answer: Option B

Explanation: A subclass is a class that extends or inherits attributes and methods from a superclass.

12.) Which type of polymorphism is resolved at runtime and is based on the actual object’s type?

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

Answer: Option B

Explanation: Run-time polymorphism, also known as dynamic polymorphism, is resolved at runtime based on the actual object’s type.

13.) QuesWhat is method overloading in Java?tion

A) Creating multiple methods with the same name but different parameters
B) Hiding methods from the superclass
C) Calling a method from within another method
D) Creating methods inside the main method

Answer: Option A

Explanation: Method overloading allows a class to have multiple methods with the same name but different parameter lists, enhancing code flexibility.

14.) Can a subclass access private members (fields and methods) of its superclass in Java?

A) Yes, always
B) Yes, only if they are declared as static
C) No, never
D) Yes, only if they are declared as public

Answer: Option C

Explanation: A subclass cannot directly access private members of its superclass; they are only accessible within the superclass itself.

15.) What is the “instanceof” operator used for in Java?

A) To create a new instance of a class
B) To compare two objects for equality
C) To check if an object is null
D) To test if an object is of a particular class type

Answer: Option D

Explanation: The “instanceof” operator is used to determine if an object is an instance of a specified class or a class that inherits from it.

16.) Which method is called first when an object of a subclass is created in Java?

A) The constructor of the subclass
B) The constructor of the superclass
C) The main method
D) The finalize method

Answer: Option B

Explanation: The constructor of the superclass is called first during the creation of an object of a subclass, followed by the constructor of the subclass.

17.) What is the purpose of method hiding in Java inheritance?

A) To prevent a subclass from inheriting methods
B) To override superclass methods with new implementations
C) To expose private methods of the superclass
D) To define methods with the same name in both the superclass and 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.

18.) In Java, can a class inherit from multiple classes (achieve multiple inheritance)?

A) Yes, always
B) Yes, but only if the classes are in the same package
C) No, never
D) Yes, but only if the classes have the same attributes

Answer: Option C

Explanation: Java does not support multiple class inheritance. A class can inherit from only one superclass.

19.) What is a constructor chaining in Java?

A) Calling the superclass constructor from a subclass constructor
B) Creating multiple constructors with the same parameters
C) Hiding the constructor of the superclass
D) Calling a method from within a constructor

Answer: Option A

Explanation: Constructor chaining involves calling a constructor from another constructor, typically to reuse initialization logic from the superclass.

20.) What is method overloading based on in Java?

A) The return type of methods
B) The method names
C) The parameter types and/or the number of parameters
D) The access modifiers of methods

Answer: Option C

Explanation: Method overloading is based on having multiple methods with the same name but different parameter types and/or a different number of parameters.