Java OOPs Coding Questions and Answers

Welcome to the Java OOPs Coding Quiz. In this quiz, we present 20 coding MCQ questions to test your coding knowledge on the Java OOPs topic. Each question has a correct and brief explanation.

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

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

Answer:

b) Bark

Explanation:

This is an example of method overriding. The sound() method in Dog overrides the method in the Animal class, so it prints "Bark".

2. What does this Java code snippet output?

class MyClass {
    private int x;
    public MyClass(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
}
public class Test {
    public static void main(String args[]) {
        MyClass obj = new MyClass(5);
        System.out.println(obj.getX());
    }
}
a) 0
b) 5
c) Compilation error
d) Runtime error

Answer:

b) 5

Explanation:

The constructor of MyClass sets the value of x to 5, and getX() returns this value.

3. Identify the output of the following code:

abstract class Shape {
    abstract void draw();
}
class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}
public class Test {
    public static void main(String args[]) {
        Shape shape = new Circle();
        shape.draw();
    }
}
a) Drawing Circle
b) Compilation error
c) An abstract method error
d) None of the above

Answer:

a) Drawing Circle

Explanation:

The abstract method draw() in Shape is implemented in Circle. When called, it prints "Drawing Circle".

4. What will be printed by this Java code?

interface A {
    int VAL = 5;
    void foo();
}
class B implements A {
    public void foo() {
        System.out.println("VAL in B: " + VAL);
    }
}
public class Test {
    public static void main(String args[]) {
        B obj = new B();
        obj.foo();
    }
}
a) VAL in B: 0
b) VAL in B: 5
c) Compilation error
d) Runtime error

Answer:

b) VAL in B: 5

Explanation:

VAL is a constant in the interface A. When B implements A, it inherits this constant. Thus, it prints "VAL in B: 5".

5. What does this code snippet output?

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

Answer:

b) Derived::show() called

Explanation:

Although b is a reference of type Base, it refers to an object of type Derived. Hence, Derived's show() is called.

6. What is the result of executing this code?

class Base {
    Base() {
        System.out.println("Base Constructor Called");
    }
}
class Derived extends Base {
    Derived() {
        System.out.println("Derived Constructor Called");
    }
}
public class Test {
    public static void main(String args[]) {
        Derived d = new Derived();
    }
}
a) Derived Constructor Called
b) Base Constructor Called

c) Base Constructor Called

Derived Constructor Called

d) Derived Constructor Called Base Constructor Called

Answer:

c) Base Constructor Called

Derived Constructor Called

Explanation:

When an instance of Derived is created, the constructor of Base is called first, followed by the constructor of Derived.

7. What will the following Java code snippet output?

class A {
    void methodA() {
        System.out.println("method of Class A");
    }
}
class B extends A {
    void methodB() {
        System.out.println("method of Class B");
    }
}
public class C extends B {
    public static void main(String args[]) {
        A a = new C();
        a.methodA();
    }
}
a) method of Class A
b) method of Class B
c) Compilation error
d) Runtime error

Answer:

a) method of Class A

Explanation:

The reference type A can only call methods defined in A or overridden methods in C.

8. What does the following code snippet print?

class Example {
    static {
        System.out.println("static block");
    }
    Example() {
        System.out.println("constructor");
    }
}
public class Test {
    public static void main(String[] args) {
        Example obj = new Example();
    }
}
a) constructor
b) static block
c) static block
constructor
d) constructor
static block

Answer:

c) static block
constructor

Explanation:

Static blocks are executed when the class is loaded, which is before any object creation. Therefore, it prints "static block" followed by "constructor".

9. Determine the output of this Java code:

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:

Since the type of reference a is A, a.i refers to the i in class A, not B.

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

class Outer {
    private static String msg = "Hello World";
    static class Nested {
        void hello() {
            System.out.println(msg);
        }
    }
}
public class Test {
    public static void main(String args[]) {
        Outer.Nested nested = new Outer.Nested();
        nested.hello();
    }
}
a) Hello World
b) Compilation error
c) An error about accessing a private member
d) No output

Answer:

a) Hello World

Explanation:

Nested static classes can access private static members of the outer class.

11. What will this Java code snippet output?

interface Printable {
    void print();
}
class A implements Printable {
    public void print() {
        System.out.println("A");
    }
}
class B implements Printable {
    public void print() {
        System.out.println("B");
    }
}
public class Test {
    public static void main(String args[]) {
        Printable p = new B();
        p.print();
    }
}
a) A
b) B
c) Compilation error
d) Runtime error

Answer:

b) B

Explanation:

The reference p of type Printable points to an object of class B, so B's print() method is called.

12. Identify the output of this code:

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

Answer:

c) A
B

Explanation:

When a B object is created, the constructor of A is called first, then the constructor of B.

13. What does this Java code snippet output?

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

Answer:

c) A::foo
B::foo

Explanation:

The foo() method in B calls super.foo(), which invokes A's foo(), followed by its own print statement.

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

final class A {
    final void show() {
        System.out.println("A::show");
    }
}
class B extends A {
    void show() {
        System.out.println("B::show");
    }
}
public class Test {
    public static void main(String[] args) {
        B b = new B();
        b.show();
    }
}
a) A::show
b) B::show
c) Compilation error
d) Runtime error

Answer:

c) Compilation error

Explanation:

Class A is declared final, so it cannot be extended. Also, a final method cannot be overridden.

15. What will the following code snippet print?

class A {
    void method() {
        System.out.println("Method of Class A");
    }
}
class B extends A {
    @Override
    void method() {
        System.out.println("Method of Class B");
    }
}
public class Test {
    public static void main(String args[]) {
        A obj = new B();
        obj.method();
    }
}
a) Method of Class A
b) Method of Class B
c) Compilation error
d) Runtime error

Answer:

b) Method of Class B

Explanation:

The method() in class B overrides the method in class A. Since obj is an instance of B, the overridden method in B is called.

16. What is the result of executing this code?

class Person {
    String name;
    Person(String name) {
        this.name = name;
    }
}
class Employee extends Person {
    Employee() {
        super("Unknown");
        System.out.println("Employee's name: " + name);
    }
}
public class Test {
    public static void main(String args[]) {
        Employee e = new Employee();
    }
}
a) Employee's name: Unknown
b) Employee's name: null
c) Compilation error
d) Runtime error

Answer:

a) Employee's name: Unknown

Explanation:

The Employee constructor calls the Person constructor with "Unknown" and then prints the name.

17. What will the following Java code snippet output?

abstract class Shape {
    abstract double area();
}
class Circle extends Shape {
    private double radius;
    Circle(double radius) {
        this.radius = radius;
    }
    double area() {
        return Math.PI * radius * radius;
    }
}
public class Test {
    public static void main(String args[]) {
        Shape shape = new Circle(2.0);
        System.out.println("Area: " + shape.area());
    }
}
a) Area: 12.56
b) Area: 6.28
c) Compilation error
d) Runtime error

Answer:

a) Area: 12.56

Explanation:

The area() method in Circle correctly calculates the area of the circle with a radius of 2.0.

18. Identify the output of this code:

interface MyInterface {
    default void show() {
        System.out.println("Default Method");
    }
}
class MyClass implements MyInterface {
    public void show() {
        System.out.println("Overridden Method");
    }
}
public class Test {
    public static void main(String args[]) {
        MyInterface obj = new MyClass();
        obj.show();
    }
}
a) Default Method
b) Overridden Method
c) Compilation error
d) Runtime error

Answer:

b) Overridden Method

Explanation:

MyClass overrides the default show() method of MyInterface, so the overridden version is called.

19. What does this Java code snippet output?

class A {
    void display() {
        System.out.println("Class A");
    }
}
class B extends A {
    void display() {
        System.out.println("Class B");
    }
}
public class Test {
    public static void main(String[] args) {
        A obj1 = new A();
        A obj2 = new B();
        obj1.display();
        obj2.display();
    }
}
a) Class A
Class A
b) Class A
Class B
c) Class B
Class B
d) Compilation error

Answer:

b) Class A
Class B

Explanation:

obj1 calls display() of A, while obj2 calls the overridden display() in B.

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

class Outer {
    int outer_x = 100;
    void test() {
        Inner inner = new Inner();
        inner.display();
    }
    class Inner {
        void display() {
            System.out.println("outer_x = " + outer_x);
        }
    }
}
public class Main {
    public static void main(String args[]) {
        Outer outer = new Outer();
        outer.test();
    }
}
a) outer_x = 0
b) outer_x = 100
c) Compilation error
d) Runtime error

Answer:

b) outer_x = 100

Explanation:

The Inner class has access to the members of the Outer class. When display() is called, it prints the value of outer_x from Outer.

Comments