AlgoMaster Logo

Inheritance - Quiz

Last Updated: January 3, 2026

Inheritance Exercises

31 quizzes

1
Code Completion

Make the Dog class inherit the behavior of the Animal class

java
1
class Dog Animal {

Click an option to fill the blank:

2
Code Completion

Call the parent class constructor to initialize the title field

java
1
super();

Click an option to fill the blank:

3
Code Completion

Override the start method in the ElectricCar subclass

java
1
@Override
2
public void () {

Click an option to fill the blank:

4
Multiple Choice

Which statement best describes inheritance in Java?

5
Multiple Choice

What does the extends keyword indicate in a class declaration?

6
Multiple Choice

Which use of super is valid inside a subclass method?

7
Multiple Choice

When a subclass overrides a non-static method from its superclass, which method is called at runtime?

8
Multiple Choice

What happens if a subclass constructor does not explicitly call super(...)?

9
Multiple Choice

Which class is at the top of the inheritance hierarchy in Java?

10
Multiple Choice

Which statement about the equals method from Object is true if a class does NOT override it?

11
Multiple Choice

Which expression correctly tests whether an Animal reference refers to a Dog object?

12
Multiple Choice

Which methods in a subclass cannot override methods from its superclass?

13
Multiple Choice

In a constructor, what is required if you use super(...)?

14
Multiple Choice

Which statement about instanceof and null is correct?

15
Sequencing

Order the steps to create a subclass that overrides a method from its superclass.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to properly use constructor chaining with super in an inheritance hierarchy.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code?

1class Vehicle {
2    String name = "Vehicle";
3    void printName() {
4        System.out.println(name);
5    }
6}
7class Car extends Vehicle {
8    String name = "Car";
9    @Override
10    void printName() {
11        System.out.println(name);
12    }
13}
14public class Main {
15    public static void main(String[] args) {
16        Vehicle v = new Car();
17        v.printName();
18    }
19}
18
Output Prediction

What is the output of this code?

1class Animal {
2    void sound() {
3        System.out.println("Animal");
4    }
5}
6class Dog extends Animal {
7    @Override
8    void sound() {
9        super.sound();
10    }
11}
12public class Main {
13    public static void main(String[] args) {
14        Dog d = new Dog();
15        d.sound();
16    }
17}
19
Output Prediction

What is the output of this code?

1class Base {
2    Base() {
3        System.out.print("Base");
4    }
5}
6class Derived extends Base {
7    Derived() {
8        System.out.print("Derived");
9    }
10}
11public class Main {
12    public static void main(String[] args) {
13        new Derived();
14    }
15}
20
Output Prediction

What is the output of this code?

1class Person {
2    public String toString() {
3        return "Person";
4    }
5}
6public class Main {
7    public static void main(String[] args) {
8        Person p = new Person();
9        System.out.println(p.toString());
10    }
11}
21
Output Prediction

What is the output of this code?

1class Animal {}
2class Dog extends Animal {}
3public class Main {
4    public static void main(String[] args) {
5        Animal a = new Dog();
6        boolean result = a instanceof Dog;
7        System.out.println(result);
8    }
9}
22
Bug Spotting

Find the bug in this inheritance-based class design that should compare employees by id.

Click on the line(s) that contain the bug.

java
1
class Employee {
2
    private int id;
3
    public Employee(int id) {
4
        this.id = id;
5
    }
6
    public boolean equals(Employee other) {
7
        return this.id == other.id;
8
    }
9
}
10
public class Main {
11
    public static void main(String[] args) {
12
        Employee e1 = new Employee(1);
13
        Employee e2 = new Employee(1);
14
        System.out.println(e1.equals(e2));
15
    }
16
}
23
Bug Spotting

Find the bug related to calling the superclass constructor.

Click on the line(s) that contain the bug.

java
1
class Vehicle {
2
    private String type;
3
    public Vehicle(String type) {
4
        this.type = type;
5
    }
6
}
7
class Car extends Vehicle {
8
    private String model;
9
    public Car(String model) {
10
        this.model = model;
11
    }
12
}
13
public class Main {
14
    public static void main(String[] args) {
15
        Car car = new Car("Sedan");
16
        System.out.println("Done");
17
    }
18
}
24
Matching

Match each Object class method with its primary purpose.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each inheritance-related keyword or operator with its role.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code so that Developer inherits from Employee and correctly uses the superclass constructor.

java
1
class Employee {
2
String name;
3
Employee(String name) {
4
this.name = name;
5
}
6
}
7
class Developer Employee {
8
String language;
9
Developer(String name, String language) {
10
(name);
11
this.language = language;
12
}
13
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code so that the overridden start method in Car still calls the Vehicle implementation first.

java
1
class Vehicle {
2
void start() {
3
System.out.println("Vehicle starting");
4
}
5
}
6
class Car extends Vehicle {
7
@Override
8
void start() {
9
.start();
10
System.out.println("Car ready");
11
}
12
}
13
public class Main {
14
public static void main(String[] args) {
15
Vehicle v = new Car();
16
v.();
17
}
18
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to override toString and use instanceof for safe casting.

java
1
class Animal {}
2
class Cat extends Animal {
3
String name;
4
Cat(String name) { this.name = name; }
5
@Override
6
public String () {
7
return "Cat:" + name;
8
}
9
}
10
public class Main {
11
public static void printIfCat(Animal a) {
12
if (a Cat) {
13
Cat c = (Cat) a;
14
System.out.println(c.());
15
}
16
}
17
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code so that the Manager class correctly chains constructors with this and super.

java
1
class Person {
2
String name;
3
Person(String name) {
4
this.name = name;
5
}
6
}
7
class Manager extends Person {
8
int teamSize;
9
Manager(String name) {
10
(name, 0);
11
}
12
Manager(String name, int teamSize) {
13
(name);
14
this.teamSize = teamSize;
15
}
16
}

Click an option to fill blank 1:

30
Hotspot Selection

Click the line where the overridden method in Dog is declared.

Click on the line to select.

java
1
class Animal {
2
    void speak() {
3
        System.out.println("Animal");
4
    }
5
}
6
class Dog extends Animal {
7
    @Override
8
    void speak() {
9
        System.out.println("Dog");
10
    }
11
}
12
public class Main {
13
    public static void main(String[] args) {
14
        Animal a = new Dog();
15
        a.speak();
16
    }
17
}
31
Hotspot Selection

Click the line where the instanceof operator is used to check the runtime type.

Click on the line to select.

java
1
class Shape {}
2
class Circle extends Shape {}
3
public class Main {
4
    public static void main(String[] args) {
5
        Shape s = new Circle();
6
        if (s instanceof Circle) {
7
            System.out.println("Circle detected");
8
        }
9
    }
10
}