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. 

Answer: 

A. It's a process where one class acquires the properties (fields) and behaviors (methods) of another class.

Explanation: 

Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPS (Object-Oriented Programming System) and allows for code reusability and method overriding.

2. Which keyword is used for inheritance in Java?

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

Answer: 

A. extends 

Explanation: 

The 'extends' keyword is used to create a child class from a parent class in Java.

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

A. Yes 
B. No 

Answer: 

B. No 

Explanation: 

In Java, a subclass cannot inherit the constructor of its superclass. 

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 

Answer: 

A. The class that inherits from another class 

Explanation: 

The subclass, or derived class, is the class that inherits properties from another class. 

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

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

Answer: 

A. Object 

Explanation: 

In Java, Object is the parent class of all classes. 

6. Can we perform multiple inheritance in Java? 

A. Yes 
B. No 

Answer: 

B. No 

Explanation: 

Java does not support multiple inheritance with classes. 

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 

Answer: 

A. To call the constructor of the parent class 

Explanation: 

The 'super' keyword in Java is used to call the constructor and methods of the parent class. 

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

A. Yes 
B. No 

Answer: 

B. No 

Explanation: 

In Java, a subclass cannot inherit private members of its superclass. 

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 

Answer: 

C. A class extends another class which also extends another class 

Explanation: 

Multilevel inheritance occurs when a class extends another class which also extends another class. 

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

A. Yes 
B. No 

Answer: 

A. Yes 

Explanation: 

Yes, interfaces can be used to achieve multiple inheritance in Java.

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. 

Answer: 

B. No, static methods cannot be overridden because they belong to the class, not the object. 

Explanation: 

In Java, a static method is bound to the class and not to the instance. Hence, it is not part of the state of the object and doesn't participate in polymorphism and dynamic dispatch, which are necessary for method overriding.

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 

Answer: 

B. "The car moves" 

Explanation: 

In Java, a subclass can override methods from its superclass. In this example, the Car class is overriding the move method of the Vehicle class. Since the object is instantiated as a Car, the overridden move method in the Car class is called, producing the output "The car moves".

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" 

Answer: 

C. "parent from child" 

Explanation: 

In Java, while methods are overridden (dynamic binding), variables are not overridden (static binding). Therefore, p.name refers to the Parent class variable, and p.message() refers to the Child class method.

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 

Answer: 

C. "Grandparent's Print()" 

Explanation: 

In Java, a subclass inherits all the members (fields, methods, and nested classes) from its superclass. Here, the Child class indirectly inherits the print() method from the Grandparent class through the Parent class. Therefore, it's able to call print().

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

Answer:

a) Static Method

Explanation:

The provided Java code will compile and execute successfully without any exceptions. When calling a static method, it doesn't require an instance of the class. Therefore, you can call the static method staticMethod() from class First using the null reference first.

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

Answer:

c) One,Two,Three

Explanation:

When we create an object of class Three, the constructors are executed in the following order: 
One(): Prints "One,". 
Two(): Prints "Two,". 
Three(): Prints "Three". 
So, the overall output is "One,Two,Three". The constructors are executed in the order of inheritance hierarchy from the topmost superclass (One) to the subclass (Three).

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 

Answer: 

C. Constructor of FirstClass, Constructor of SecondClass, Constructor of ThirdClass 

Explanation: 

When an instance of ThirdClass is created, the constructor of FirstClass (the superclass) is called first, followed by the constructor of SecondClass (the intermediate class), and finally, the constructor of ThirdClass (the most derived class).

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 

Answer: 

B) ChildClass Display 

Explanation: 

In Java, static methods are not subject to the normal rules of polymorphism, which apply to instance methods. This is referred to as "method hiding". So, when we call the static displayMessage() method using ChildClass, the static displayMessage() method of ChildClass will be executed, hence "ChildClass Display" will be printed to the console.

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 

Answer: 

D) Alpha Beta Gamma 

Explanation: 

In Java, when we create an instance of a subclass (like ClassGamma), the instance initializers (or initializer blocks) of its superclass chain are executed in order from the topmost superclass downwards. Therefore, the output will be "Alpha Beta Gamma", with each word on a new line.

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 

Answer: 

A) 10 

Explanation: 

In Java, fields are not overridden like methods. If a subclass has a field that shares the same name as a field in its superclass, then the two fields are considered to be different. They do not override each other. The output of this program is 10 because the reference type (ClassOne) determines which variableOne is accessed, not the actual object type (ClassTwo).

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