Object-Oriented Programming (OOP) interview questions

25 questions from the Object-Oriented Programming (OOP) course, each with the correct answer and an explanation. Try to answer before you open the answer.

Classes and Objects

1. Which list names the four commonly cited pillars of OOP?

  1. Encapsulation, abstraction, inheritance, polymorphism
  2. Variables, loops, functions, arrays
  3. Compilation, linking, loading, execution
  4. Classes, files, packages, modules
Show answer

Answer: Encapsulation, abstraction, inheritance, polymorphism

These four principles are the classic foundation of object-oriented design.

Lesson: Why Object-Oriented Programming

2. After `Student b = a;` in Java, what does b refer to?

  1. A new copy of the object
  2. Nothing until it is initialised
  3. The Student class itself
  4. The same object that a refers to
Show answer

Answer: The same object that a refers to

Assignment copies the reference, so both variables point to one object.

Lesson: Classes, Objects, State and Identity

3. What happens to Java's default no-argument constructor once you write any constructor for a class?

  1. It is still generated automatically
  2. It becomes static
  3. It is no longer provided; you must write it yourself if you need it
  4. It becomes private
Show answer

Answer: It is no longer provided; you must write it yourself if you need it

Java only supplies the default constructor when the class declares no constructors.

Lesson: Constructors, this and Static Members

Encapsulation and Abstraction

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

  1. public
  2. protected
  3. private
  4. package-private (no modifier)
Show answer

Answer: private

private restricts access to the declaring class.

Lesson: Encapsulation and Access Modifiers

5. Why does an immutable class make defensive copies of mutable constructor arguments such as lists?

  1. To save memory
  2. So the caller cannot change the object's state later through the list it still holds
  3. Because Java requires it
  4. To make the class faster
Show answer

Answer: So the caller cannot change the object's state later through the list it still holds

Without a copy, the caller's reference to the same list could modify the object.

Lesson: Immutable Objects

6. Which is true about Java interfaces and abstract classes?

  1. A class can implement several interfaces but extend only one class
  2. A class can extend several abstract classes
  3. Interfaces can hold instance fields
  4. Abstract classes can be instantiated with new
Show answer

Answer: A class can implement several interfaces but extend only one class

Java supports single class inheritance and multiple interface implementation.

Lesson: Abstraction, Abstract Classes and Interfaces

Inheritance

7. In a Java subclass constructor, where must an explicit call to super(...) appear?

  1. As the first statement
  2. Anywhere in the constructor
  3. As the last statement
  4. Only in static methods
Show answer

Answer: As the first statement

The parent must be initialised before the subclass constructor body runs, so super(...) comes first.

Lesson: Inheritance, super and Constructor Chaining

8. Which change is allowed when overriding a public method in Java?

  1. Making the override private
  2. Changing the parameter types
  3. Throwing a broader checked exception
  4. Returning a subtype of the original return type
Show answer

Answer: Returning a subtype of the original return type

Covariant return types are allowed; narrower access, different parameters or broader checked exceptions are not.

Lesson: Method Overriding Rules

9. How does Java handle a class that implements two interfaces with conflicting default methods?

  1. It picks one at random
  2. It is a runtime error
  3. The class must override the method and resolve the conflict explicitly
  4. The first interface listed always wins
Show answer

Answer: The class must override the method and resolve the conflict explicitly

The compiler requires the class to override and may call InterfaceName.super.method().

Lesson: When Inheritance Goes Wrong

Polymorphism

10. Which pair of Java methods is a valid overload?

  1. int total(int a) and double total(int a)
  2. void run() and private void run()
  3. void log(String s) and void log(String s, int level)
  4. static void x() and void x()
Show answer

Answer: void log(String s) and void log(String s, int level)

Overloads must differ in their parameter lists; return type or modifiers alone are not enough.

Lesson: Compile-Time Polymorphism: Overloading

11. When is the method body to execute chosen for an overridden method called through a superclass reference in Java?

  1. At compile time, from the reference's declared type
  2. At run time, from the actual object's class
  3. When the class is loaded
  4. It is chosen randomly
Show answer

Answer: At run time, from the actual object's class

Dynamic dispatch selects the implementation of the object's actual class at run time.

Lesson: Run-Time Polymorphism and Dynamic Dispatch

12. What is duck typing?

  1. An object is usable if it has the required methods, regardless of its declared type
  2. A class must declare which interfaces it implements
  3. Only primitive types can be used
  4. A naming convention for Python classes
Show answer

Answer: An object is usable if it has the required methods, regardless of its declared type

Duck typing checks behaviour (available methods) rather than declared type.

Lesson: Programming to Interfaces and Duck Typing

Relationships Between Classes

13. An Order creates its OrderLine objects, and they cannot exist without it. Which relationship is this?

  1. Composition
  2. Association
  3. Aggregation
  4. Dependency
Show answer

Answer: Composition

Strong ownership with a shared lifetime is composition.

Lesson: Association, Aggregation and Composition

14. What is a main advantage of composition over inheritance?

  1. It always runs faster
  2. It removes the need for interfaces
  3. It makes all fields public
  4. Behaviour can be combined and changed by swapping collaborators, with less coupling to internals
Show answer

Answer: Behaviour can be combined and changed by swapping collaborators, with less coupling to internals

Composition depends on interfaces of parts, which can be swapped and combined flexibly.

Lesson: Composition Over Inheritance and Delegation

15. In UML, how is composition shown?

  1. A dashed line with a hollow triangle
  2. A solid line with a hollow triangle
  3. A solid line with a filled diamond at the whole's end
  4. A dashed arrow
Show answer

Answer: A solid line with a filled diamond at the whole's end

Composition uses a filled diamond; aggregation uses a hollow one.

Lesson: UML Class Diagrams

SOLID Design Principles

16. According to the Open/Closed Principle, how should a new payment method usually be added?

  1. Edit a large switch statement in existing code
  2. Copy the whole payment module
  3. Add a new class implementing an existing interface
  4. Make all fields public
Show answer

Answer: Add a new class implementing an existing interface

OCP favours extension through new implementations rather than modifying tested code.

Lesson: Single Responsibility and Open/Closed

17. Which is a sign of a Liskov Substitution Principle violation?

  1. A subclass adds a new helper method
  2. A subclass throws UnsupportedOperationException for a method the parent supports
  3. A subclass overrides toString
  4. A class implements two interfaces
Show answer

Answer: A subclass throws UnsupportedOperationException for a method the parent supports

Refusing behaviour the parent promises breaks substitutability.

Lesson: Liskov Substitution and Interface Segregation

18. What does the Dependency Inversion Principle recommend?

  1. Both high-level and low-level modules should depend on abstractions
  2. High-level modules should create low-level modules directly
  3. All classes should be static
  4. Avoid interfaces to reduce code
Show answer

Answer: Both high-level and low-level modules should depend on abstractions

DIP inverts the dependency so business logic depends on interfaces, not concrete details.

Lesson: Dependency Inversion and Dependency Injection

Object Behaviour: Equality, Lifecycle and Errors

19. If a Java class overrides equals but not hashCode, what typically goes wrong?

  1. Equal objects may land in different hash buckets, so HashSet and HashMap lookups fail
  2. The class no longer compiles
  3. equals stops being called
  4. Objects become immutable
Show answer

Answer: Equal objects may land in different hash buckets, so HashSet and HashMap lookups fail

The equals-hashCode contract requires equal objects to have equal hash codes.

Lesson: Identity, Equality and Hashing

20. Two Team objects were created by a shallow copy. What happens if one adds a member to its members list?

  1. Only that team changes
  2. A compile error occurs
  3. The garbage collector removes the list
  4. Both teams see the new member, because they share the same list
Show answer

Answer: Both teams see the new member, because they share the same list

Shallow copies share referenced objects such as the list.

Lesson: Object Lifecycle, Copying and Resources

21. Which exception best signals that a method was called while the object is in the wrong state?

  1. IllegalArgumentException
  2. ArithmeticException
  3. IllegalStateException
  4. ClassNotFoundException
Show answer

Answer: IllegalStateException

IllegalStateException indicates the call is not valid for the object's current state.

Lesson: Exceptions and Design by Contract

Object-Oriented Design in Practice and Revision

22. In the parking lot design, why is pricing modelled as an interface?

  1. Interfaces are faster than classes
  2. Java requires interfaces for numbers
  3. Pricing rules vary, so an interface lets new strategies be added without changing ParkingLot
  4. To make Vehicle abstract
Show answer

Answer: Pricing rules vary, so an interface lets new strategies be added without changing ParkingLot

Variation points behind interfaces follow the Open/Closed and Dependency Inversion principles.

Lesson: From Requirements to Classes

23. Which language implements interfaces structurally, so a type satisfies an interface just by having its methods?

  1. Java
  2. Go
  3. C#
  4. C++
Show answer

Answer: Go

Go interfaces are satisfied implicitly by any type with the required method set.

Lesson: OOP Across Languages

24. What is an anaemic domain model?

  1. Classes holding data with getters and setters while business rules live elsewhere, so objects cannot protect their invariants
  2. A model with too many methods
  3. A database with no tables
  4. A model that uses only static classes for performance
Show answer

Answer: Classes holding data with getters and setters while business rules live elsewhere, so objects cannot protect their invariants

Separating all behaviour from data loses the main benefit of encapsulation.

Lesson: Common OOP Mistakes

25. Which concept allows a superclass reference to call the subclass's version of a method at run time?

  1. Method overloading
  2. Encapsulation
  3. Static binding of fields
  4. Run-time polymorphism through overriding (dynamic dispatch)
Show answer

Answer: Run-time polymorphism through overriding (dynamic dispatch)

Dynamic dispatch chooses the overriding method of the actual object at run time.

Lesson: Revision and Interview Questions

← Back to the course