Back
Decor-dark

65+ Best Object-Oriented Programming (OOP) Interview Questions

6124 words Read time 29:36

It’s never a good idea to go into an interview unprepared. While you may have the best experience and a portfolio showing that you’re an expert, the more information you have on the questions you may be asked, the better. This is especially true when it comes to object-oriented programming (OOP) interview questions.

While some OOP interview questions may seem unnecessary, they actually tell the interviewer that:
• You came prepared
• You know how to relay your knowledge
• Your understanding is at a certain level (e.g. expert, intermediate)

Ultimately, the manner in which you answer these questions will determine whether you’ll get the job, and/or what kind of position you’ll get. To help you prepare as much as possible, we’ve compiled all the possible questions you may be asked in your next OOP interview. Let’s take a look!

Object-Oriented Programming Interview Questions Table of Contents

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.

1. What are object-oriented programming, a class, and an object?

Object-oriented programming is a programming language model that relies on objects (as opposed to actions), and data (as opposed to logic). These objects typically contain data and interact with one another, which is why they often have self-modifying features once the proper procedures have been put into place. Classes are representations of object types, and objects are instances of class with their own state, behavior and identity.

2. What are the most common OOP languages?

Java, C++, C#, Python, PHP, Ruby.

3. What can OOP be based on?

Typically, it’s based on classes, which are representations of object types. However, OOP can also be prototype-based.

4. What’s the difference between class-based and prototype-based objects in OOP, and what’s an abstract class?

When OOP is class-based, we create objects and then sort them into appropriate classes with shared attributes. For example, if we have apples and oranges, we can expect both to have the attributes of fruit (have a color, taste, etc.). However, when we use the prototype-based objects, the objects are our main point of focus. There are no classes, and we observe objects as standalone entities. Finally, an abstract class is a class that can’t be instantiated.

5. What are the principles of object-oriented programming?

The main four principles of object-oriented programming are: encapsulation, abstraction, inheritance and polymorphism. Abstraction is sometimes not considered a principle since it’s an extent of encapsulation.

6. What is encapsulation in OOP?

Encapsulation is a way of keeping an object’s state data hidden from other objects. For example, if apple is an object and it’s going rotten, then with encapsulation no other object can change its rotting state. It can only water it, i.e. communicate with it via specified methods.

Encapsulation is performed through creating private and public variables. Private variables are the ones only the object can access. On the other hand, public variables allow other objects to communicate with it through determined public functions that can lead to the modification of an object’s private attributes.

There are also protected and internal variables, which allow only certain objects to modify an object’s data, or restrict the access to data only to objects that share the same class.

7. What is abstraction in OOP?

Since objects can contain a lot of data, it has to be compressed for increased efficiency. That’s exactly what abstraction does. It allows OOPs to share only the data necessary for software’s functioning, while keeping the rest of the details hidden.

For example, if I’m an object, I won’t tell you everything about my family because it’s not relevant to the situation and it wastes resources like time. However, I’ll tell you about my work history because it’s necessary for this interview to function.

Or, consider how we use our cars. We know that they run, we can drive, but we don’t know a lot about what’s going on under the hood because all we need to know is the mechanism: the car helps us get from point A from point B.

8. What is inheritance in object-oriented programming, and what is multiple inheritance?

Since objects in OOP can be very similar and share some logic and attributes while not being the exact same, we need a way to connect them while distinguishing them at the same time.

If we want to use common logic that’s shared by multiple objects while allowing each object to retain its unique logic, we can use inheritance.

We do this by creating child and parent classes. Just like with DNA, we distinguish between common logic in the parent class and the unique logic in the child class.

For example, we may share our parents’ sense of humor, but not their painting skills. Multiple inheritance is when there are multiple parent classes, so at runtime it creates the diamond problem: which parent class’ behavior should an object extend?

9. What is polymorphism in OOP?

Polymorphism is a way of allowing different objects in different classes to use the same methods (typically determined by a parent class). So while their attributes are different, they can still use the same methods that will be applied in accord with their attributes. If we have CalculateSurface function, and we want to apply it to three different objects which have different parameters, we don’t have to define it for each one of them. Instead, we can copy the method from the parent class and apply it to each object.

Then each object can calculate surface while implementing its own parameter data, different from the parameter data other objects have.

10. Why do we use object-oriented programming?

Because of its principles that make it leaner.

They allow us to reuse code instead of duplicating it, saving time and reducing storage. Additionally, OOP is more flexible and allows code to be changed without a lot of fuss.

11. What is dynamic dispatch, and why is it better than static dispatch?

Dynamic dispatch or run-time dispatch is much more flexible than static dispatch.

If we’re using static dispatch, we have to predetermine what function will be dispatched when we call up a method, and what value will be attributed to a variable.

On the other hand, if we’re using dynamic dispatch, we can determine this at run time. We can also combine or swap out components. Subclasses can override the methods and behaviors of base classes through open recursion.

12. Can objects contain other objects in their instance variables?

Yes! That’s called object composition, and it helps us create complex objects in a leaner way.

13. What’s the difference between object composition and inheritance in OOP?

Both let us reuse code.

However, composition lets us to reuse the code without extending it, while inheritance – specifically, in Java – requires us to extend the class if we want to retain the original functionality.

14. Can you explain the open-closed principle in object-oriented programming?

The open/closed principle states that classes should be open to extension, but closed for modification. So while we can add on with inheritance, the source code shouldn’t be modified.

15. What are manipulators, and how are they used?

Manipulators are functions typically used for tasks such as changing formatting parameters and inserting or removing special characters.

16. What are constructors and destructors in OOP, and how are they used?

They’re methods used in object-oriented programming when we want to start up or shut down an object.

Constructors allocate memory when we construct class-based objects, and they establish initial values so objects can initialize right.

There are three types of constructors:
Default constructor, which has no parameters.
Parametric constructor, which creates new instances and passes arguments simultaneously at creation.
Finally, there is the copy constructor which creates a copy of the existing object.
And when we want to destroy objects, especially if they’ve created other objects, we use destructors so there’s no memory leak.

17. What are virtual, friend and inline functions?

Virtual functions help us define target functions, even if we haven’t defined the target.

For example, if we’re executing a virtual function drink, base classes (for example, animal) and each subclass (for example, foxes and rabbits) would execute their standard behavior without us having to manually execute functions and behaviors for each.

Friend functions are functions that get access to private and protected data of a class, even though they’re not in that class. We typically use them when we want a function to access the private data of two different objects.

Finally, inline functions are “summarized” detailed, complete functions that only expand when called on. However, they’re not expanded when they’re not used, which saves us from wasting time and space.

18. What’s the difference between procedural programming and object-oriented programming?

First, OOP is just leaner. It encourages the reuse of code and extensions, and it saves space. Procedural is more complex, which makes it a lot harder to implement changes and maintain it.

Additionally, OOP encapsulates data and focuses on object states and behaviors, whereas procedural exposes data and prioritizes the sequence of function execution.

19. What are function and operator overloading, and what is a ternary operator?

If we want to create multiple functions with the same name that are implemented in different ways, we use function overloading. This is handy because we can execute one function call and have different tasks performed.

The main two rules for function overloading are that the same function name should be used for more than one function definition, and that the functions should be different either in arity or parameter types.

Operator overloading, on the other hand, is a case of polymorphism. Depending on their arguments, different operators have different implementations.

A ternary operator is a conditional operator that has three operands or arguments. It’s marked with a question mark and a colon. It checks whether the three values are true before delivering the result.

20. What two types of arguments do we have in object-oriented programming?

The first is the call by value. It passes arguments to a function while copying the value of the argument into the parameter of the function.

The second is the call by reference. This one copies the address of the argument’s value into the parameter of the function.

21. What is exception handling?

Exception handling is a process that handles any exceptions (like error exceptions) that can occur during the execution. We use it in programs like C++ and Java to maintain the execution flow despite exceptions occurring.

Or, to put it simply, exception handling allows us to make a Plan B for when exceptions happen.

22. What are method overriding and the super keyword?

If we want a child class to use a different implementation than its parent class, we use method overriding. So instead of using the parent class’ method, with method overriding, we can have the child class exhibit different behavior.

The super keyword allows us to invoke this overridden method.

23. So what’s the difference between overloading and overriding in OOP?

Overloading happens when multiple methods have the same name but different parameters, whereas overriding means that we have two classes: parent and child, with two methods which have the same name and parameters.

24. What are compilers and tokens?

A compiler is a program that turns statements written in a programming language to code that a computer’s processor can recognize. And tokens are language elements – the basic components of source code. There are five universally recognized types of tokens:
The first is the keyword. It’s a word that has already been given meaning in the code by the programming language, like void or return.
The second are the identifiers, which are the names of different elements.
The third are operators. They’re symbols defining operations.
The fourth are separators, which separate different programming elements, and the fifth are literals.

Literals are constant values that are used in operations and calculations. There’s typically three types of them: integers, floating point, and character literals.

25. What are access and sealed modifiers?

Access modifiers are keywords that determine the accessibility of class, methods and other members. Like I already said, in OOP we use encapsulation, and access modifiers are exactly what helps us execute it.

The number of access modifiers depends on the language we’re using. For example, C++ has only three modifiers: public, protected and private. C# has five: public, protected, internal, private, and protected internal. Java has four: public, package, protected and private. In a class, the default access modifier is private.

Sealed modifiers are modifiers used to prevent inheritance and overriding.

26. What’s the difference between virtual, sealed, override and abstract keywords in OOP?

Virtual keywords enable parent classes to be overridden in the child class, and override keywords override the virtual function in the child class.

Sealed keywords prevent the class from being overridden and inherited.

Finally, abstract keywords in object-oriented programming is used to modify the class, method and property declaration because abstract classes can’t be instantiated or called directly.

27. What are the SOLID principles in object-oriented programming?

They’re five design principles that make software easier to understand, maintain and change.

28. What is the S in SOLID?

Single responsibility principle. It states that every class or function should have a single responsibility that’s encapsulated. It’s based on the principle of cohesion.

For example, a class shouldn’t have the responsibility of modifying the content and the format of an image. According to the single responsibility principle, it should only have one responsibility.

29. What is the O in SOLID?

The O in SOLID is the open/closed principle. Open for extension, closed for modification so that the source code doesn’t change.

30. What is the L in SOLID?

The L in SOLID is the Liskov substitution principle.

This one states that objects should be replaceable with subtype instances without breaking the code so that the execution flow of an application is maintained.

31. What is the I in SOLID?

The I is the interface segregation principle.

The interface segregation principle states that it’s better to have multiple client-specific interfaces than one “Jack of all trades,” general-purpose interface.

It’s another instance of the lean approach of object-oriented programming’s leanness. No client should depend on the methods that they don’t use.

32. What is the D in SOLID?

It’s the dependency inversion principle, which says that everything should depend on abstractions.

This principle pertains to software module decoupling, and it inverts the typical dependency relationships so that the high-level modules aren’t depended on low-level module details.

Instead, both should depend on the same abstraction.

33. What is the difference between is-a, and has-a relationships in OOP?

Is-a relationships are inheritance, and has-a relationships are composition.

Is-a means that the child class is a type of parent class, so we simply extend the class. It’s a static, compile-time binding.

Has-a relationship, on the other hand, requires us to create instances that refer to other objects. It’s a dynamic, runtime binding.

34. What is GRASP in object-oriented design?

GRASP is an acronym that stands for General Responsibility Assignment Software Principles. They further regulate the assigning of responsibilities to classes and objects in OOP.

There are nine of them: controller, creator, indirection, information expert, high cohesion, low coupling, polymorphism, protected variations, and pure fabrication.

35. What is the controller in OOP?

The controller assigns the responsibility of dealing with system events. The controller object is a non-UI class object, and it can be used for more use cases.

36. What is the creator in OOP?

Since creation is so common in object-oriented programming, we have to decide on class responsibilities when it comes to object creation. The creator determines the relationship of objects and classes, if the set requirements are satisfied.

37. What is high cohesion in OOP?

It’s a low-coupling principle that states objects should be focused, manageable and understandable. High cohesion stands for creating a system of different objects focused on singular responsibilities, whereas low cohesion means that too few elements have too many responsibilities.

Low cohesion is against the fundamental OOP principles, as this state makes the code hard to reuse, change and maintain. That’s why we use high cohesion.

38. What is the expert principle?

It’s a principle used to delegate responsibilities such as methods. The best approach is to define the responsibility and the information necessary to fulfill it, and then find where the information is stored.

To put it simply: the expert (or information expert) principle places the responsibility on the class that has the most information required to fulfill the responsibility.

39. What is low coupling?

A principle that defines how we’re going to delegate responsibility in order to have lower dependency between classes, reduced impact on the second class when we make changes to the first, and a higher reuse potential.

40. What is the polymorphism principle?

Instead of too much code and explicit branching, the polymorphism principle states that we should simply use polymorphic operations for variations of behaviors.

41. What’s the protected variations principle in OOP?

This principle protects elements like objects and systems from the variations made on other elements. This is done through the interface segregation principle and polymorphism.

42. What is pure fabrication?

It’s a class that doesn’t represent a concept, it’s also called a service in domain-driven design.

43. What is the indirection principle?

This final GRASP principle chooses an intermediate object to which we’ll assign the responsibility of mediation between two elements. It supports low coupling.

44. What is responsibility-driven design in OOP, and what are its building blocks?

Responsibility-driven design is an object-oriented programming technique that improves encapsulation by defining classes around their responsibility and the information they share.

Its building blocks are: application (software is defined as a set of interacting objects), candidate objects (key concepts described on CRC cards), collaborations (interaction of objects), CRC cards (index cards that are used to record candidates), hot spots (points where variations occur), and hot spot cards (similar to CRC cards, consist of: name, description of the variation, and examples of occurrences).

45. What are the responsibility usage scenarios in responsibility-driven design?

Public responsibilities – services and information an object provides to others.
Private responsibilities – actions that an object takes in order to fulfill public responsibilities.
Subresponsibilities – smaller responsibilities that make up a large responsibilities. For example, subordinate responsibilities and sequencing responsibilities.

46. What roles can objects have in responsibility-driven design in OOP?

Controller (the object with this role makes decisions for other objects), coordinator (delegates tasks), information holder (provides information), interface (communication with other applications, within object neighborhoods, and with users), service provider (computing services), and structurer (maintains relationships and information about them).

47. What is data-driven design in OOP?

Unlike responsibility-driven design, data-driven OOP stands for defining classes around the data they need to hold. However, due to the nature of object-oriented programming and its benefits, responsibility-driven design is more efficient.

48. Why is PHP an object-oriented programming language?

PHP is an object-oriented programming language because it has the main OOP features like abstraction, inheritance, and others. It was also based on C++. Additionally, PHP classes have methods and properties that can be used to create objects with their own properties and values.

49. What is a namespace in PHP?

It’s a function used for encapsulation, and it’s similar to abstraction. It establishes relationships between classes. They’re declared with the namespace keyword.

50. When do we use a final keyword in PHP?

We use it when we don’t want a class to be extended, or when we want to stop child classes from overriding their parent class’ method.

51. Why is Java an object-oriented programming language?

Because it’s a collection of objects, and supports the primary OOP concepts like inheritance, encapsulation, polymorphism and others.

52. When are local and instance variables defined in Java?

Local variables are defined in the method, and instance variables are defined in the class.

53. What is the difference between Array and Array Lists in Java?

With array, size is determined at the time of declaration, while array lists don’t require the size to be defined. Instead, it’s changed dynamically.

If we want to put an object in array, we need to specify the index. If we want to do same in the array lists, we don’t need an index.Finally, array is not type-parametrized, and array lists are.

54. What is a group of objects called in Java?

A collection.

55. What interfaces are available in Java collections?

Collection, list, set, map, sorted set, sorted map, and queue.

56. What classes are available in Java collections?

In Java, they’re called lists, and there are three types: array lists, vectors, and linked lists.

57. What is the flow of execution called in Java?

It’s called a Thread. Every program has at least one thread: the main thread. We can create other threads by either extending the main thread or using the runnable interface.

58. What is monkey patching in Python?

It’s a way to change the behavior of functions or objects after they’ve been defined. It’s usually used in testing, but it’s much better to use the mock package for this purpose.

59. What are decorators in Python OOP?

Decorators are functions that modify the functionality of other functions. They can be used for authorization and logging.

60. What is garbage collection in Python?

Automatic memory allocation and deallocation. It uses reference counting to determine whether an object should still be live or removed. If there aren’t enough references by other objects to a particular objects, it will be removed.

61. What is the difference between deep and shallow copy in Python?

Shallow copy is used at instance type creation when a new instance type keeps the values from the original instance. It also copies reference pointers.

Deep copy, on the other hand, doesn’t copy the reference pointers. It makes the reference to an object.

62. Why is C# an OOP language?

Because C# offers full support for the object-oriented programming concepts.

63. What are the differences between interface and abstract class in C#?

An abstract class in C# can have concrete methods, but all the methods in an interface have to be abstract. Unlike an abstract class, an interface can’t declare and use any variables.

When it comes to accessibility, in abstract classes access is private by default but can be changed. In an interface, it’s public and can’t be changed.

64. What happens if inherited interfaces have conflicting method names?

We have to use the interface name before the method name. Otherwise, we can’t use them independently in the same class.

65. What are extension methods in C#?

They’re static methods that can be called as though they were instance methods.

This is a way of adding methods to types that already exist, without the need for creation of a new type or modification of the original type.

66. What is a state-related codebase in C#?

It’s an approach to coding that replaces branching and conditional statements with functions. These functions can later be used to manage state within a class.

Instead of writing conditional statements which will check whether the conditions have been satisfied, we can delegate the responsibility to a different class. If its function hasn’t been fulfilled, there’s no need to call a class which would check the conditions.

67. What is the difference between the object methods clone and dup in Ruby?

Clone will copy module methods, while dup just creates a shallow copy of an object.

68. How many types of variables are used in Ruby?

There are four types of variables in ruby: class (which start with @@), instance (@), global ($), and local variables (not prefixed).

Share this article

C

Other articles