Search Tutorials


Java Inheritance MCQ - Multiple Choice Questions And Answers | JavaInUse

Java Inheritance MCQ - Multiple Choice Questions And Answers

Q. Which keyword is used in Java to implement inheritance between classes?

A. extends
B. 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 C
B. 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. Yes
B. No

Q. Which access modifier is used to allow access to a member in the subclass only?

A. public
B. 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 sound
B. 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 reuse
B. 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 classes
B. 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 inheritance
B. 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 superclass
B. 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 superclass
B. 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





Q. What is the correct syntax to define a class that inherits from another class in Java?

A.
class ChildClass extends ParentClass {
    // class implementation
}
B.
class ParentClass extends ChildClass {
    // class implementation
}
C.
class ChildClass implements ParentClass {
    // class implementation
}
D.
class ParentClass implements ChildClass {
    // class implementation
}

Q. Which of the following statements about Java inheritance is true?

A.
Inheritance allows a class to inherit properties and behaviors from multiple classes.
B.
Inheritance is not supported in Java.
C.
Inheritance allows a class to inherit properties and behaviors from another class.
D.
Inheritance is only used for creating abstract classes.

Q. What is the keyword used in Java to prevent a class from being inherited?

A.
abstract
B.
final
C.
static
D.
private

Q. Which of the following statements about Java inheritance is false?

A.
A subclass can access the private members of its superclass.
B.
A subclass can override the methods of its superclass.
C.
A subclass can inherit the constructors of its superclass.
D.
A subclass can have its own unique members.

Q. Which of the following statements about method overriding in Java is true?

A.
Method overriding is a mechanism for creating multiple methods with the same name but different parameters in a class.
B.
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
C.
Method overriding is only used for static methods.
D.
Method overriding is not supported in Java.

Q. What is the output of the following Java code snippet?

class Parent {
    public void print() {
        System.out.println("Parent");
    }
}

class Child extends Parent {
    public void print() {
        System.out.println("Child");
        super.print();
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.print();
    }
}
A.
Parent
Child
B.
Child
Parent
C.
Parent
D.
Child

Q. Which of the following is not a valid type of inheritance in Java?

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

Q. Which of the following statements about the super keyword in Java is true?

A.
The super keyword is used to call the parent class constructor.
B.
The super keyword is used to access the methods and properties of the parent class.
C.
The super keyword is used to create an instance of a child class.
D.
The super keyword is not supported in Java.

Q. What is the purpose of using the protected access modifier in Java?

A.
To restrict access to the class only within the same package.
B.
To make the class and its members accessible to any class in the same package and its subclasses.
C.
To make the class and its members accessible to any class in any package.
D.
To make the class and its members accessible only within the same class.