OOPs Online Test

Welcome to OOPs (Object-Oriented Programming) Online Test!. Here, we will present 25 MCQs (Multiple-Choice Questions) containing code snippets to test your Java coding and logical skills.

You can select the correct answer for each question and submit the test. You will get your online test score after finishing the complete test.

1. What is the purpose of the super keyword in Java?

a) To call methods of the superclass
b) To declare static methods
c) To define a variable
d) To instantiate objects

2. Which principle of OOP does the following Java code demonstrate?

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}
class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction

3. What is encapsulation in Java?

a) Breaking down a program into smaller parts
b) Protecting data by restricting access to methods
c) The concept of combining methods and attributes into a single unit
d) Providing access to all fields and methods freely

4. What does the following Java code snippet indicate?

interface Flyable {
    void fly();
}
class Bird implements Flyable {
    public void fly() {
        System.out.println("Bird flies");
    }
}
a) Multiple inheritance
b) Single inheritance
c) Polymorphism
d) Abstraction

5. Consider the following Java code. What design pattern is illustrated here?

class Singleton {
    private static Singleton instance = new Singleton();
    private Singleton() {}
    public static Singleton getInstance() {
       return instance;
    }
}
a) Factory Method
b) Singleton
c) Adapter
d) Prototype

6. In Java, what is an abstract class?

a) A class that cannot be instantiated and must be inherited
b) A class that allows for dynamic method resolution
c) A fully implemented class
d) A class that only contains abstract methods

7. What is the output of this Java program?

abstract class Animal {
    abstract void makeSound();
    void eat() {
        System.out.println("Animal is eating");
    }
}
class Dog extends Animal {
    void makeSound() {
        System.out.println("Bark");
    }
}
public class Test {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.makeSound();
        myDog.eat();
    }
}
a) Animal is eating\nBark
b) Bark\nAnimal is eating
c) Compilation error
d) None of the above

8. What principle of OOP is violated by the following Java code?

class Vehicle {
    void start() {
        System.out.println("Vehicle starts");
    }
    void stop() {
        System.out.println("Vehicle stops");
    }
}
class Car extends Vehicle {
    void start() {
        System.out.println("Car starts");
    }
    void stop() {
        System.out.println("Car stops");
    }
    void openTrunk() {
        System.out.println("Trunk is opened");
    }
}
a) Encapsulation
b) Open/Closed Principle
c) Liskov Substitution Principle
d) Single Responsibility Principle

9. Which concept in OOP allows changing the behavior of a superclass in its subclass?

a) Encapsulation
b) Inheritance
c) Polymorphism
d) Composition

10. What is the result of the following code?

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

11. In Java, which keyword is used to prevent a method from being overridden?

a) final
b) static
c) private
d) protected

12. What is the principle of "composition over inheritance"?

a) Using interfaces instead of abstract classes
b) Prefering inherited classes for code reuse
c) Using member variables that are instances of other classes instead of inheritance
d) Avoiding interfaces in favor of abstract classes

13. What concept does the following Java code illustrate?

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);
        }
    }
}
a) Encapsulation
b) Inheritance
c) Inner class
d) Polymorphism

14. How does the interface keyword enhance a Java program?

a) It allows multiple inheritance.
b) It provides methods with default implementations.
c) It ensures that classes derive from only one base class.
d) It forces classes to provide implementations for certain methods.

15. What will happen when you try to compile and run the following Java code?

abstract class Machine {
    abstract void run();
}
public class Car extends Machine {
}
a) Compiles and runs without output
b) Compiles but throws an exception when run
c) Does not compile
d) Compiles and prints "Machine is running"

16. What does the @Override annotation do in Java?

a) It indicates that a method declaration is intended to override a method declaration in the superclass.
b) It changes the behavior of a base class method.
c) It hides the superclass method.
d) It creates a new method in the subclass.

17. What principle of object-oriented programming is being used when different classes are accessed through the same interface?

a) Encapsulation
b) Abstraction
c) Polymorphism
d) Inheritance

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

class A {
    int num = 100;
}
class B extends A {
    int num = 200;
    void printNum() {
        System.out.println(super.num);
    }
}
public class Main {
    public static void main(String[] args) {
        B b = new B();
        b.printNum();
    }
}
a) 100
b) 200
c) Compilation error
d) None of the above

19. What is the significance of the private access modifier in Java?

a) It restricts access to the member so that it can only be accessed within its own class.
b) It allows the member to be accessed from anywhere in the program.
c) It restricts the member to be accessed only by classes that are defined within the same package.
d) It allows the member to be accessed by any subclass.

20. How do you achieve data hiding in Java?

a) By using private access modifiers
b) By using public access modifiers
c) By using protected access modifiers
d) By not using any access modifiers

21. Consider the following Java code. What OOP concept does it demonstrate?

interface Drawable {
    void draw();
}
class Circle implements Drawable {
    public void draw() {
        System.out.println("Drawing a circle");
    }
}
class Square implements Drawable {
    public void draw() {
        System.out.println("Drawing a square");
    }
}
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction

22. What is the advantage of using an abstract class in Java?

a) To create a fully abstract model that cannot be instantiated
b) To provide a partial implementation of an interface
c) To enforce that certain methods are overridden by subclasses
d) All of the above

23. What will the following Java code output?

class Base {
    void display() {
        System.out.println("Base display()");
    }
}
class Derived extends Base {
    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) None of the above

24. What is the purpose of the static keyword when declaring methods in Java?

a) To allow the methods to be called on the class itself, rather than on instances of the class
b) To make methods faster by storing them in static memory
c) To restrict the visibility of methods within the same package
d) To ensure that only one instance of the method can exist

25. How is exception handling implemented in Java?

    try {
    // Code that might throw an exception
    } catch (Exception e) {
        // Code to handle the exception
    } finally {
        // Code that is always executed
    }
a) Using if-else statements
b) Using try-catch-finally blocks
c) Using protected methods
d) Using error return values

Comments