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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare