Java OOPS Quiz - Coding Programs

This page contains Java OOPS quiz (coding programs) questions on OOPS concepts Abstraction, Encapsulation, Inheritance, and Polymorphism.

These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.
Check out Part 1 - Java OOPS Quiz - Java Interview OOPS Programs - Part 1
Learn OOPs at OOPS Concepts in Java with Realtime Examples 
The answer and explanation of each question have given at the end of each question.


Q1 - What is the output of the following application?

class Automobile {
    private final String drive() {
        return "Driving vehicle";
    }
}

class Car extends Automobile {
    protected String drive() {
        return "Driving car";
    }
}

public class ElectricCar extends Car {

    @Override
    public final String drive() {
        return "Driving electric car";
    }

    public static void main(String[] wheels) {
        final Car car = new ElectricCar();
        System.out.print(car.drive());
    }
}
A. Driving vehicle
B. Driving electric car
C. Driving car
D. The code does not compile

Q2 - What is the output of the following application?

interface Run {
    default void walk() {
        System.out.print("Walking and running!");
    }
}

interface Jog {
    default void walk() {
        System.out.print("Walking and jogging!");
    }
}

public class Sprint implements Run, Jog {
    public void walk() {
        System.out.print("Sprinting!");
    }

    public static void main(String[] arg) {
        new Sprint().walk();
    }
}

Q3 - What will be the output of the following program?

class Base {
    public Base() {
        System.out.println("Base");
    }
}

class Derived extends Base {
    public Derived() {
        System.out.println("Derived");
    }
}

class DeriDerived extends Derived {
    public DeriDerived() {
        System.out.println("DeriDerived");
    }
}

public class Test {
    public static void main(String[] args) {
        Derived b = new DeriDerived();
    }
}
a)
Base
Derived
DeriDerived
b)
Derived
DeriDerived
c)
DeriDerived
Derived
Base
d)
DeriDerived
Derived

Q4 - Consider the following program:

class Base {
    public Base() {
        System.out.print("Base ");
    }

    public Base(String s) {
        System.out.print("Base: " + s);
    }
}

class Derived extends Base {
    public Derived(String s) {
        super(); // Stmt-1
        super(s); // Stmt-2
        System.out.print("Derived ");
    }
}

class Test {
    public static void main(String[] args) {
        Base base = new Derived("Hello ");
    }
}
Select three correct options from the following list:
a) Removing Stmt-1 will make the program compilable and it will print the following: Base Derived.
b) Removing Stmt-1 will make the program compilable and it will print the following: Base: Hello Derived.
c) Removing Stmt-2 will make the program compilable and it will print the following: Base Derived.
d) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the following: Base Derived.
e) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the following: Base: Hello Derived.

Q5 - What is the output of the following application?

abstract class Car {
    static {
        System.out.print("1");
    }

    public Car(String name) {
        super();
        System.out.print("2");
    }

    {
        System.out.print("3");
    }
}

public class BlueCar extends Car {
    {
        System.out.print("4");
    }

    public BlueCar() {
        super("blue");
        System.out.print("5");
    }

    public static void main(String[] gears) {
        new BlueCar();
    }
}
A. 23451
B. 12354
C. 13245
D. The code does not compile.

Q6 - What is the output of the following application?

class Math {
    public final double secret = 2;
}

class ComplexMath extends Math {
    public final double secret = 4;
}

public class InfiniteMath extends ComplexMath {
    public final double secret = 8;

    public static void main(String[] numbers) {
        Math math = new InfiniteMath();
        System.out.print(math.secret);
    }
}
A. 2
B. 4
C. 8
D. The code does not compile.

Q7 - Consider the following program:

public class Overloaded {
    public static void foo(Integer i) {
        System.out.println("foo(Integer)");
    }

    public static void foo(short i) {
         System.out.println("foo(short)");
    }

    public static void foo(long i) {
        System.out.println("foo(long)");
    }

    public static void foo(int... i) {
         System.out.println("foo(int ...)");
    }

    public static void main(String[] args) {
       foo(10);
    }
}
Which one of the following options correctly describes the output of this program? a) foo(Integer)
b) foo(short)
c) foo(long)
d) foo(int ...)

Answers

Q1

Answer :
B. Driving electric car
Explanation: The drive() method in the Car class does not override the version in the Automobile class since the method is not visible to the Car class. Therefore, the final attribute in the Automobile class does not prevent the Car class from implementing a method with the same signature.
The drive() method in the ElectricCar class is a valid override of the method in the Car class, with the access modifier expanding in the subclass. For these reasons, the code compiles, and Option D is incorrect. 
In the main() method, the object created is an ElectricCar, even if it is assigned to a Car reference. Due to polymorphism, the method from the ElectricCar will be invoked, making Option B the correct answer.

Q2

Answer :
C
Explanation: Having one class implement two interfaces that both define the same default method signature leads to a compiler error, unless the class overrides the default method. In this case, the Sprint class does override the walk() method correctly, therefore the code compiles without issue, and Option C is correct.

Q3

Answer :
a) Base
Derived
DeriDerived
Explanation: Whenever a class gets instantiated, the constructor of its base classes (the constructor of the root of the hierarchy gets executed first) gets invoked before the constructor of the instantiated class.

Q4

Answer:
b) Removing Stmt-1 will make the program compilable and it will print the following:
Base: Hello Derived.

c) Removing Stmt-2 will make the program compilable and it will print the following:
Base Derived.

d) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print
the following: Base Derived.

Explanation: If you remove Stmt-1, a call to super(s) will result in printing Base: Hello, and then the constructor of the Derived class invocation will print Derived. Similarly, the removal of Stmt-2 will also produce the correct program. In fact, if you remove both these statements, you will also get a compilable program.

Q5

Answer:
C
Explanation: The class is loaded first, with the static initialization block called and 1 is outputted first. When the BlueCar is created in the main() method, the superclass initialization happens first. The instance initialization blocks are executed before the constructor, so 32 is outputted next. Finally, the class is loaded with the instance initialization blocks again being called before the constructor, outputting 45. The result is that 13245 is printed, making Option C the correct answer.

Q6

Answer:
A
Explanation: The code compiles without issue, so Option D is incorrect. Java allows methods to be overridden, but not variables. Therefore, marking them final does not prevent them from being reimplemented in a subclass. Furthermore, polymorphism does not apply in the same way it would to methods as it does to variables. In particular, the reference type determines the version of the secret variable that is selected, making the output 2 and Option A the correct answer.

Q7

Answer:
c) foo(long)
Explanation: For an integer literal, the JVM matches in the following order: intlongIntegerint.... In other words, it first looks for an int type parameter; if it is not provided, then it looks for long type; and so on. Here, since the int type parameter is not specified with an overloaded method, it matches with foo(long).

Comments