Java Online Test - OOPs Concepts

Dive into the core of Java programming with our specially curated quiz on Object-Oriented Programming (OOP) concepts. This set of 25 coding-focused multiple-choice questions will challenge your understanding of inheritance, polymorphism, encapsulation, and more. Test your skills and deepen your knowledge of Java OOP to enhance your coding expertise.

1. What is the output of the following Java program?

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

2. How can you prevent a class from being subclassed?

a) By declaring the class as static
b) By using the final keyword with the class
c) By using the final keyword with all methods
d) By making the constructor private

3. What is the output of the following Java program?

class Animal {
   void makeSound() {
       System.out.println("Generic Animal Sound");
   }
}
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();
   }
}
a) Generic Animal Sound
b) Bark
c) Compilation error
d) No output

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

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

5. What is the use of the super keyword in Java?

a) To refer to the superclass variables and methods
b) To create instances of a class
c) To declare static methods
d) To declare final methods

6. Which method signature is an example of method overloading?

a) public void print(String message)
b) public void print(int number)
c) public int print(String message)
d) Both a and b

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

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

8. In Java, which keyword is used to inherit properties and methods from a class?

a) super
b) extends
c) implements
d) this

9. Which OOP concept encapsulates data within a class and provides controlled access through getter/setter methods

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

10. What will be the output of the following Java program?

class Base {
    Base() {
        System.out.print("Base Constructor");
    }
}
class Derived extends Base {
    Derived() {
        System.out.print("Derived Constructor");
    }
}
public class Main {
    public static void main(String[] args) {
        new Derived();
    }
}
a) Base ConstructorDerived Constructor
b) Derived ConstructorBase Constructor
c) Base Constructor
d) Derived Constructor

11. Which keyword can be used for an interface to inherit another interface in Java?

a) extends
b) implements
c) super
d) this

12. What does the following Java code illustrate?

abstract class Machine {
    abstract void start();
}
class Car extends Machine {
    void start() {
        System.out.println("Car starts with a key.");
    }
}
a) Concrete implementation of an abstract class
b) Use of final methods in Java
c) Static method inheritance
d) None of the above

13. When does method overriding occur?

a) When a subclass uses a method with the same name and signature as a method in its superclass
b) When two methods have the same name but different parameters
c) When a class defines a static method with the same name as a static method in its superclass
d) None of the above

14. What is polymorphism in Java?

a) The ability of a variable, function, or object to take on multiple forms
b) A concept where a variable, function, or object can exist in multiple scopes at the same time
c) A technique of encrypting the code
d) The process of versioning of methods

15. What feature of OOP is used to hide the internal workings of an object and only expose what is necessary?

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

16. Which of the following is not a type of constructor in Java?

a) Default constructor
b) No-argument constructor
c) Parameterized constructor
d) Copy constructor

17. What will the following code snippet output?

public class Test {
    private float f = 1.0f;
    int m() {
        return f;
    }
    public static void main(String args[]) {
        Test t = new Test();
        t.m();
    }
}
a) 1.0
b) 1
c) Compilation error
d) No output

18. What is the concept of using @Override annotation in Java?

a) It is used to override method definitions
b) It informs the compiler that the element is meant to override an element declared in a superclass.
c) It is used to define a new method in a subclass
d) It is a special annotation used to speed up the runtime performance

19. In Java, which of these allows classes to have more than one superclass?

a) Multithreading
b) Multiple inheritance through classes
c) Multiple inheritance through interfaces
d) Polymorphic extension

20. Consider the following Java class definition. What is this an example of?

public final class MyFinalClass {}
a) Inheritance
b) Encapsulation
c) Finalization
d) Prevention of inheritance

21. What does the following Java code snippet represent?

interface Payable {
    void pay();
}
class Employee implements Payable {
    public void pay() {
        System.out.println("Employee paid");
    }
}
a) Encapsulation
b) Interface implementation
c) Concrete class definition
d) Abstract class usage

22. What would be the result of the following code if it compiles and runs?

public class Calculation {
    void sum(int a, long b) { System.out.println("a method invoked"); }
    void sum(long a, int b) { System.out.println("b method invoked"); }
    public static void main(String args[]) {
        Calculation obj = new Calculation();
        obj.sum(20, 20);  // line 5
    }
}
a) a method invoked
b) b method invoked
c) Compilation error: ambiguous method call
d) None of the above

23. How is a static method in a superclass accessed?

a) Through the superclass name
b) Through an instance of the superclass
c) Through an instance of the subclass
d) Both a and b are correct

24. Which of the following is not a valid statement about abstract classes in Java?

a) They can have constructors
b) They cannot have method implementations
c) They can have static methods
d) They can have final methods

25. What happens if a superclass method does not have an access modifier, and the subclass defines the overriding method with a public access modifier?

a) It results in a compilation error
b) The subclass method successfully overrides the superclass method
c) It leads to runtime exceptions
d) The method in the subclass is not considered an overridden method

Comments