Skip to content
dronacharya.ai

OOP interview questionswith answers and explanations

22 original OOP questions of the kind asked in technical MCQ rounds and interviews, each with the answer and a short explanation. To answer them yourself first, open the CS fundamentals practice.

questions
22
easy
12
medium
7
hard
3
  1. Question 1

    Easy

    Which concept lets a subclass provide its own implementation of a method already defined in its parent class?

    1. AOverloading
    2. BOverridingAnswer
    3. CEncapsulation
    4. DAbstraction

    Explanation

    Overriding replaces an inherited method with the subclass's version, chosen at runtime. Overloading is the same name with different parameters.

  2. Question 2

    Easy

    Making a class's fields private and giving access only through public getter and setter methods is an example of:

    1. AInheritance
    2. BPolymorphism
    3. CMultiple dispatch
    4. DEncapsulationAnswer

    Explanation

    Encapsulation bundles data with the methods that use it and controls access to that data, so the class can check or change how a field is read and written.

  3. Question 3

    Easy

    What is method overloading?

    1. AA subclass replacing a method it inherited
    2. BSeveral methods with the same name but different parameter lists in one classAnswer
    3. CA method that calls itself
    4. DA field hiding a field of the same name in the parent class

    Explanation

    Overloaded methods share a name and differ in the number or types of parameters, as with print(int) and print(String). Replacing an inherited method is overriding.

  4. Question 4

    Easy

    Method overloading is an example of which kind of polymorphism?

    1. ACompile-time (static) polymorphismAnswer
    2. BRun-time (dynamic) polymorphism
    3. CMultiple inheritance
    4. DEncapsulation

    Explanation

    The compiler picks the overload from the argument types at compile time. Overriding, resolved from the object's actual type when the program runs, is run-time polymorphism.

  5. Question 5

    Easy

    A Java class cannot extend more than one class. Which feature lets a class take on several types instead?

    1. AImplementing multiple interfacesAnswer
    2. BDeclaring the class abstract
    3. CNesting inner classes
    4. DAdding static methods

    Explanation

    A Java class can implement any number of interfaces, which gives it multiple types without the ambiguity of inheriting state and code from two parent classes.

  6. Question 6

    Easy

    Which statement about an abstract class in Java is true?

    1. AIt can contain only abstract methods
    2. BIt can be instantiated with new like any other class
    3. CIt cannot be instantiated, and it can have both abstract and concrete methodsAnswer
    4. DIt cannot have a constructor

    Explanation

    An abstract class cannot be created with new, but it can hold fields, constructors (called by subclasses) and fully written methods alongside abstract ones.

  7. Question 7

    Medium

    What does this Java code print?

    class A { void show() { System.out.print("A"); } }
    class B extends A { void show() { System.out.print("B"); } }
    
    // in main:
    A obj = new B();
    obj.show();
    1. ABAnswer
    2. BA
    3. CAB
    4. DCompilation error

    Explanation

    show() is an instance method that B overrides. The call is resolved from the object's actual type, B, not from the variable's type, so it prints B.

  8. Question 8

    Hard

    What does this Java code print?

    class A { static void show() { System.out.print("A"); } }
    class B extends A { static void show() { System.out.print("B"); } }
    
    // in main:
    A obj = new B();
    obj.show();
    1. AB
    2. BAB
    3. CAAnswer
    4. DCompilation error

    Explanation

    Static methods are not overridden, only hidden. A static call is bound at compile time from the declared type of obj, which is A, so it prints A. (It compiles, with a warning about calling a static method through an instance.)

  9. Question 9

    Medium

    What does this Java code print?

    class P { P() { System.out.print("P"); } }
    class C extends P { C() { System.out.print("C"); } }
    
    // in main:
    new C();
    1. ACP
    2. BPCAnswer
    3. CC
    4. DP

    Explanation

    Every constructor first calls its parent's constructor (an implicit super()). So P's constructor prints P before C's body prints C.

  10. Question 10

    Easy

    Which Java access modifier makes a member visible only inside its own class?

    1. Aprotected
    2. Bpublic
    3. CNo modifier (package-private)
    4. DprivateAnswer

    Explanation

    private limits access to the declaring class. No modifier allows the whole package, protected adds subclasses, and public allows everyone.

  11. Question 11

    Medium

    In C++, what does declaring a member function virtual do?

    1. AIt makes the function callable only from derived classes
    2. BIt stops derived classes from overriding the function
    3. CCalls through a base-class pointer or reference run the derived class's version, chosen at run timeAnswer
    4. DThe function is chosen at compile time from the pointer's type

    Explanation

    A virtual function is dispatched through the object's vtable, so Base* p = new Derived; p->f(); runs Derived::f. Without virtual, the pointer's static type decides.

  12. Question 12

    Medium

    What is a pure virtual function in C++?

    1. AA virtual function declared with = 0, which makes its class abstractAnswer
    2. BA function that can never be overridden
    3. CA static member function
    4. DAn inline function with an empty body

    Explanation

    virtual void draw() = 0; declares a function with no implementation in that class. A class with one cannot be instantiated, and derived classes must override it to become concrete.

  13. Question 13

    Easy

    A Car has an Engine. Which relationship models this best?

    1. AInheritance (is-a)
    2. BComposition (has-a)Answer
    3. CMethod overloading
    4. DMultiple inheritance

    Explanation

    A car is not a kind of engine, it contains one, so Car should hold an Engine field (composition) rather than extend Engine.

  14. Question 14

    Medium

    Which statement about Java interfaces (Java 8 and later) is true?

    1. AAn interface can declare instance fields that each object stores
    2. BA class can implement only one interface
    3. CInterface methods are private unless marked otherwise
    4. DAn interface can have default methods with a bodyAnswer

    Explanation

    Since Java 8, interfaces can have default and static methods with bodies. Fields in an interface are always public static final constants, and interface methods are public by default.

  15. Question 15

    Medium

    Which SOLID principle says that an object of a subclass should work anywhere an object of its parent class is expected, without breaking the program?

    1. ALiskov substitution principleAnswer
    2. BSingle responsibility principle
    3. COpen/closed principle
    4. DDependency inversion principle

    Explanation

    The Liskov substitution principle: subtypes must honour the parent's contract. A Square that breaks Rectangle's independent width and height setters is the usual counterexample.

  16. Question 16

    Easy

    What does the open/closed principle say?

    1. AAll fields should be public, and all methods private
    2. BClasses should be open for extension but closed for modificationAnswer
    3. CA class should have only one constructor
    4. DCode should be closed source

    Explanation

    New behaviour should come from adding code, such as a new subclass or strategy, rather than editing a tested class.

  17. Question 17

    Easy

    In Java, what does the keyword this refer to inside an instance method?

    1. AThe parent class
    2. BThe class's static members
    3. CThe object the method was called onAnswer
    4. DThe most recently created object

    Explanation

    this is a reference to the current object. It is often used to tell a field from a parameter with the same name, as in this.name = name.

  18. Question 18

    Easy

    Which design pattern makes sure a class has only one instance and gives a global point of access to it?

    1. AFactory method
    2. BObserver
    3. CSingletonAnswer
    4. DDecorator

    Explanation

    A singleton hides its constructor and hands out one shared instance through a static method, as with a single configuration or logging object.

  19. Question 19

    Medium

    An object holds a reference to a list. After a shallow copy of the object, what do the copy and the original share?

    1. ANothing, because every nested object was copied too
    2. BThe same list object, because only the reference was copiedAnswer
    3. COnly their primitive fields
    4. DTheir methods, but none of their fields

    Explanation

    A shallow copy copies field values, and for a reference field the value is the reference. Both objects then point to one list, so a change through either shows in both. A deep copy would copy the list too.

  20. Question 20

    Hard

    What does this Java code print?

    static void f(long x) { System.out.print("long"); }
    static void f(Integer x) { System.out.print("Integer"); }
    
    // in main:
    f(5);
    1. AInteger
    2. BCompilation error: ambiguous call
    3. Cint
    4. DlongAnswer

    Explanation

    Java resolves overloads in phases and tries widening conversions before boxing. Widening the int 5 to long succeeds in the first phase, so f(long) is chosen and it prints long.

  21. Question 21

    Hard

    In C++, why should a base class with virtual functions usually have a virtual destructor?

    1. ASo that its constructors become virtual as well
    2. BTo make the class abstract
    3. CTo stop other classes inheriting from it
    4. DSo that deleting a derived object through a base-class pointer runs the derived destructor tooAnswer

    Explanation

    With a non-virtual destructor, delete on a Base* that points to a Derived object is undefined behaviour and typically skips Derived's destructor, leaking what it owns.

  22. Question 22

    Easy

    Which OOP feature lets the same method call behave differently depending on the type of object it is called on?

    1. APolymorphismAnswer
    2. BEncapsulation
    3. CComposition
    4. DInstantiation

    Explanation

    Polymorphism: shape.area() runs the circle's formula for a Circle and the rectangle's for a Rectangle, through one common interface.

companies
222
rounds described
1,432
pieces of advice
3,771

Your next drive is closer than you think.

Start with the 15-minute mixed test: 13 questions that show where you stand in every section.