Java Inheritance Quiz - Multiple Choice Questions (MCQ)


Welcome to Java Inheritance Quiz!. This Java Inheritance Quiz consists of important 20 multiple-choice questions (MCQ) with answers and explanations. Go ahead and test your knowledge of the Java Inheritance concept.

The first 10 questions are very simple and the remaining 10 questions are medium and complex.

1. What is Inheritance in Java programming? 

A. It's a process where one class acquires the properties (fields) and behaviors (methods) of another class.
B. It's a process of creating a new class using the main() method. 
C. It's a technique to create objects in Java. 
D. It's a Java-specific term for importing packages. 

2. Which keyword is used for inheritance in Java?

A. extends 
B. new 
C. super 
D. this 

3. Can a class inherit constructors from its superclass in Java? 

A. Yes 
B. No 

4. What is a subclass in Java inheritance? 

A. The class that inherits from another class 
B. The class that is inherited from 
C. The final class in the inheritance chain 
D. None of the above 

5. What is the parent class of all classes in Java? 

A. Object 
B. String 
C. Class 
D. System 

6. Can we perform multiple inheritance in Java? 

A. Yes 
B. No 

7. What is the purpose of the 'super' keyword in Java?

A. To call the constructor of the parent class 
B. To call a method of the child class 
C. To create a new instance of a class 
D. To define a static method 

8. Can a subclass inherit private members of its superclass? 

A. Yes 
B. No 

9. What is a multilevel inheritance in Java? 

A. A class extends two or more classes 
B. Two or more classes extend the same class 
C. A class extends another class which also extends another class 
D. All of the above 

10. Can interfaces be used to achieve multiple inheritance in Java? 

A. Yes 
B. No 

11. In Java, is it possible to override a static method?

A. Yes, we can override a static method just like we do with instance methods. 
B. No, static methods cannot be overridden because they belong to the class, not the object. 
C. It depends on whether the static method is declared as final or not. 
D. It depends on the access modifier of the static method. 

12. What is the output of the following Java program?

public class Vehicle {
    public void move() {
        System.out.println("The vehicle moves");
    }
}

public class Car extends Vehicle {
    public void move() {
        System.out.println("The car moves");
    }
}

public class Main {
    public static void main(String[] args) {
        Vehicle vehicle = new Car();
        vehicle.move();
    }
}
A. "The vehicle moves" 
B. "The car moves" 
C. The code does not compile 
D. None of the above 

13. What is the output of the following Java program?

class Parent {
    String name = "parent";
    String message() {
        return "from parent";
    }
}

class Child extends Parent {
    String name = "child";
    String message() {
        return "from child";
    }
}

public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        System.out.println(p.name + " " + p.message());
    }
}
A. "parent from parent" 
B. "child from child" 
C. "parent from child" 
D. "child from parent" 

14. What is the output of the following Java program?

class Grandparent {
    public void print() {
        System.out.println("Grandparent's Print()");
    }
}

class Parent extends Grandparent { }

class Child extends Parent { }

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.print();
    }
}
A. "Child's Print()" 
B. "Parent's Print()" 
C. "Grandparent's Print()" 
D. Compilation error 

15. What will be the output of the following program?

class First
{
    static void staticMethod()
    {
        System.out.println("Static Method");
    }
}

public class MainClass
{
    public static void main(String[] args)
    {
        First first = null;

        first.staticMethod();
    }
}
a) Static Method
b) Throws a NullPointerException
c) Compile-time error
d) Runtime error

16. What is the output of the following Java program?

class One{
	public One(){
		System.out.print("One,");
	}
}
class Two extends One{
	public Two(){
		System.out.print("Two,");
	}
}
class Three extends Two{
	public Three(){
		System.out.print("Three");
	}
}

public class Test{

	public static void main(String[] args){
		Three three = new Three();
	}
}
a) Three
b) One
c) One,Two,Three
d) Run-time error

17. What is the output of the following Java program?

class FirstClass
{
    public FirstClass()
    {
        System.out.println("Constructor of FirstClass");
    }
}

class SecondClass extends FirstClass
{
    public SecondClass()
    {
        System.out.println("Constructor of SecondClass");
    }
}

class ThirdClass extends SecondClass
{
    public ThirdClass()
    {
        System.out.println("Constructor of ThirdClass");
    }
}

public class Main
{
    public static void main(String[] args)
    {
        ThirdClass instanceOfThirdClass = new ThirdClass();
    }
}
A. Constructor of ThirdClass 
B. Constructor of SecondClass 
C. Constructor of FirstClass, Constructor of SecondClass, Constructor of ThirdClass 
D. No output 

18. What is the output of the following Java program?

class ParentClass
{
    static void displayMessage()
    {
        System.out.println("ParentClass Display");
    }
}

class ChildClass extends ParentClass
{
    static void displayMessage()
    {
        System.out.println("ChildClass Display");
    }
}

public class Main
{
    public static void main(String[] args)
    {
        ChildClass.displayMessage();
    }
}
A) ParentClass Display 
B) ChildClass Display 
C) The program will not compile 
D) The program will throw a runtime exception 

19. What is the output of the following Java program?

class ClassAlpha
{
    {
        System.out.println("Alpha");
    }
}

class ClassBeta extends ClassAlpha
{
    {
        System.out.println("Beta");
    }
}

class ClassGamma extends ClassBeta
{
    {
        System.out.println("Gamma");
    }
}

public class Main
{
    public static void main(String[] args)
    {
        ClassGamma gamma = new ClassGamma();
    }
}

What will be the output when this program is run? 

A) Alpha 
B) Beta 
C) Gamma 
D) Alpha Beta Gamma 

20. What is the output of the following Java program?

class ClassOne
{
    int variableOne = 10;
}

class ClassTwo extends ClassOne
{
    int variableOne = 20;
}

public class Main
{
    public static void main(String[] args)
    {
        ClassOne objOne = new ClassTwo();

        System.out.println(objOne.variableOne);
    }
}
A) 10 
B) 20 
C) 30 
D) The program will not compile 

Conclusion

Congratulations on completing the Java Inheritance quiz! We hope this quiz has helped you assess your Java Inheritance knowledge. Feel free to review the explanations and practice more Java coding to improve your skills further. Happy coding!


Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare