Java Inheritance MCQ - Multiple Choice Questions And Answers
Q. Which keyword is used in Java to implement inheritance between classes?
A. extendsB. implements
C. inherits
D. inheritsFrom
Q. Which is an example of multilevel inheritance in Java?
A. Class A extends Class B, Class B extends Class CB. Class A extends Class B, Class A implements Interface C
C. Class A extends Class B, Class C extends Class A
D. Class A extends Class B, Class B extends Class C, Class C extends Class D
Q. In Java, can a class extend multiple classes?
A. YesB. No
Q. Which access modifier is used to allow access to a member in the subclass only?
A. publicB. private
C. protected
D. default
Q. What is the output of the following code?
class Animal {
public void makeSound() {
System.out.println("Animal is making a sound");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Dog is barking");
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.makeSound();
}
}
A. Animal is making a soundB. Dog is making a sound
C. Dog is barking
D. Compile-time error
Q. Which of the following is NOT a benefit of using inheritance in Java?
A. Code reuseB. Polymorphism
C. Encapsulation
D. Method Overloading
Q. Which of the following statements is true about the concept of "is-a" relationship in Java inheritance?
A. It represents a one-to-one relationship between classesB. It allows objects of one class to be treated as objects of another class
C. It is used to achieve method overriding
D. It can only exist between classes in the same package
Q. Which type of inheritance is demonstrated when a class inherits from a class that already inherits from another class?
A. Single inheritanceB. Multiple inheritance
C. Multilevel inheritance
D. Hierarchical inheritance
Q. What is the purpose of the super keyword in Java?
A. To access variables and methods of the superclassB. To create an instance of the superclass
C. To invoke the constructor of the superclass
D. To access variables and methods of the subclass
Q. Which of the following is NOT a restriction for using inheritance in Java?
A. A subclass cannot access private members of the superclassB. A subclass can override the final methods of the superclass
C. A subclass must call the constructor of the superclass
D. A subclass can add new methods and fields