Java Inheritance Coding Questions and Answers

Welcome to the Java Inheritance Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge of Java Inheritance. Each question has a correct and brief explanation.

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

class Animal {
    String sound() {
        return "Generic Sound";
    }
}
class Dog extends Animal {
    String sound() {
        return "Bark";
    }
}
public class Test {
    public static void main(String[] args) {
        Animal myAnimal = new Dog();
        System.out.println(myAnimal.sound());
    }
}
a) Generic Sound
b) Bark
c) Compilation error
d) Runtime error

Answer:

b) Bark

Explanation:

This is an example of method overriding in Java. The Dog class overrides the sound method of the Animal class. Even though the object is referred to by a variable of type Animal, the Dog's sound method is called due to polymorphism.

2. What does this Java code snippet output?

class A {
    int i = 10;
}
class B extends A {
    int i = 20;
}
public class Main {
    public static void main(String args[]) {
        A a = new B();
        System.out.println(a.i);
    }
}
a) 10
b) 20
c) Compilation error
d) Runtime error

Answer:

a) 10

Explanation:

Variable shadowing occurs here. The variable i in class B shadows the variable i in class A. Since the reference type of a is A, it accesses A's i, which is 10.

3. Identify the output of the following code:

class Vehicle {
    String type = "4W";
    int maxSpeed = 100;

    Vehicle(String type, int maxSpeed) {
        this.type = type;
        this.maxSpeed = maxSpeed;
    }
}

class Car extends Vehicle {
    String trans;

    Car(String trans) {
        super("4W", 150);
        this.trans = trans;
    }
}

public class Test {
    public static void main(String[] args) {
        Car c = new Car("Auto");
        System.out.println(c.type + " " + c.maxSpeed + " " + c.trans);
    }
}
a) 4W 150 Auto
b) null 0 Auto
c) 4W 100 Auto
d) Compilation error

Answer:

a) 4W 150 Auto

Explanation:

The Car constructor calls the superclass constructor Vehicle with arguments, initializing type and maxSpeed. Then it initializes its own field trans.

4. What will be printed by this Java code?

class Parent {
    void show() {
        System.out.println("Parent's show()");
    }
}
class Child extends Parent {
    void show() {
        System.out.println("Child's show()");
    }
}
public class Main {
    public static void main(String[] args) {
        Parent obj1 = new Parent();
        Parent obj2 = new Child();
        obj1.show();
        obj2.show();
    }
}
a) Parent's show()
Parent's show()
b) Parent's show()
Child's show()
c) Child's show()
d) Compilation error

Answer:

b) Parent's show()
Child's show()

Explanation:

obj1 is an instance of Parent and calls Parent's show(). obj2 is an instance of Child referred by a Parent reference and calls the overridden show() in Child.

5. What does this code snippet output?

class Base {
    static void display() {
        System.out.println("Base display()");
    }
}
class Derived extends Base {
    static void display() {
        System.out.println("Derived display()");
    }
}
public class Test {
    public static void main(String args[]) {
        Base obj = new Derived();
        obj.display();
    }
}
a) Base display()
b) Derived display()
c) Compilation error
d) Runtime error

Answer:

a) Base display()

Explanation:

Static methods are not overridden. They are hidden. obj.display() calls the display method of the Base class because the reference type of obj is Base.

6. What is the result of executing this code?

class Animal {
    Animal() {
        System.out.println("Animal is created");
    }
}
class Dog extends Animal {
    Dog() {
        System.out.println("Dog is created");
    }
}
public class Test {
    public static void main(String args[]) {
        Dog d = new Dog();
    }
}
a) Animal is created
Dog is created
b) Dog is created
Animal is created
c) Compilation error
d) Runtime error

Answer:

a) Animal is created
Dog is created

Explanation:

When an instance of a subclass is created, the constructor of the superclass is called first, followed by the constructor of the subclass.

7. What will the following Java code snippet output?

class Animal {
    void eat() throws Exception {
        System.out.println("Animal eats");
    }
}
class Dog extends Animal {
    void eat() {
        System.out.println("Dog eats");
    }
}
public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.eat();
    }
}
a) Dog eats
b) Animal eats
c) Compilation error
d) Runtime exception

Answer:

c) Compilation error

Explanation:

The eat method in Dog class cannot reduce the exception declared in the Animal class. Overridden methods must not throw checked exceptions that are new or broader than those declared by their superclass.

8. What does the following code snippet print?

class A {
    String s = "Class A";
}
class B extends A {
    String s = "Class B";

    void display() {
        System.out.println(s);
        System.out.println(super.s);
    }
}
public class Test {
    public static void main(String[] args) {
        B b = new B();
        b.display();
    }
}
a) Class B
Class A
b) Class A
Class A
c) Class B
Class B
d) Class A

Answer:

a) Class B
Class A

Explanation:

The display method in class B prints its own field s and then the field s of its superclass A.

9. Determine the output of this Java code:

interface I {
    default void display() {
        System.out.println("Interface I");
    }
}
class C implements I {
    public void display() {
        System.out.println("Class C");
    }
}
public class Test {
    public static void main(String[] args) {
        I obj = new C();
        obj.display();
    }
}
a) Interface I
b) Class C
c) Compilation error
d) Runtime error

Answer:

b) Class C

Explanation:

Class C provides its own implementation of the display method, which overrides the default method in the interface I.

10. What is the result of the following code snippet?

abstract class AbstractClass {
    abstract void abstractMethod();
    void concreteMethod() {
        System.out.println("This is a concrete method.");
    }
}
class SubClass extends AbstractClass {
    void abstractMethod() {
        System.out.println("Abstract method implementation.");
    }
}
public class Test {
    public static void main(String[] args) {
        AbstractClass obj = new SubClass();
        obj.abstractMethod();
        obj.concreteMethod();
    }
}
a) Abstract method implementation.
This is a concrete method.
b) This is a concrete method.
c) Compilation error
d) Runtime error

Answer:

a) Abstract method implementation.
This is a concrete method.

Explanation:

The SubClass provides an implementation for the abstract method of AbstractClass. Both the implemented abstract method and the concrete method are called on the SubClass instance.

11. What is the result of the following code snippet?

class ClassOne {
    void method(String s1) {
        method(s1, s1 + s1);
    }

    void method(String s1, String s2) {
        method(s1, s2, s1 + s2); //
    }

    void method(String s1, String s2, String s3) {
        System.out.println(s1 + s2 + s3);
    }
}

public class Main {
    public static void main(String[] args) {
        ClassOne one = new ClassOne();
        one.method("A");
    }
}
a) AAAAAA
b) AAAAA
c) Compilation error
d) AAA

Answer:

a) AAAAAA

Explanation:

The program demonstrates method overloading in Java within the ClassOne. The class defines three overloaded versions of the method function, each accepting a different number of string parameters. In the main method, an instance of ClassOne is created, and the overloaded method is invoked with a single string argument "A," showcasing the flexibility of method overloading in handling different parameter combinations. The output is "AAAAAA," resulting from the concatenation of the provided strings in the invoked methods.

Comments