Object Oriented Programming Interview Questions

The concepts of Object Oriented Programming are the basis for many of today's programming languages. In fact, the OOPs model is so popular, that many of the most widely used programming languages support and use this Object Oriented Programming or OOPs model , such as Java, C++, Python, C#, etc. Here, we have prepared the important object-oriented programming Interview Questions and Answers , which will help you succeed in your interview.

How much memory does a class occupy?

A class without an object requires no space allocated to it. The memory allocation takes place only when you create objects because the objects is what implements the contents of the class. So to an object of an empty class, 1 byte is allocated by compiler, for it's unique address identification. If a class have multiple objects they can have different unique memory location. When you create new class, it will call an allocation function . This function can have different implementations. This depends on the language, the language version, libraries, and configuration.

What is the meaning of "IS-A" and "HAS-A" relationship?

In OOP, IS-A relationship is completely inheritance. This means, that the child class is a type of parent class. On the other hand, HAS-A relationship is composition. Composition means creating instances which have references to other objects. Foo IS-A Bar:
public class Foo extends Bar{}
Foo HAS-A Bar:
public class Foo { private Bar bar; }

A HAS-A relationship is dynamic (run time) binding while IS-A relationship is a static (compile time) binding.

What is Dependency?

Dependency means say component Y is dependent on component X. If component X changes, then Y should change accordingly. Here, a component can be a class, a function, an interface, a method or even a field. The measure of the degree of dependency of Y on X or coupling can be strong or weak. When a class makes an instance of another class then that class is said to have dependency on another class. In Object Oriented Programming , inheritance is the strong coupling. Every change to a base class will likely cause the changes of its derived classes. Inheritance is a way of code reuse. But it creates dependency between parent class and child class and breaks information hiding philosophy and also forms a rigid class hierarchy. Composition and the use of interfaces implementation is a coupling not as strong as inheritance, yet also achieving code reuse.

Can you inherit private members of a class?

A derived class doesn't inherit access to private data members. However, it does inherit a full parent object, which contains any private members which that class declares. You just can't access the members directly, because they are private to the base class. Private members are "hidden" from all outside views, so nothing outside of the class itself can access private member variables or functions. If you want to use such members in derived classes, there need to be accessor functions (e.g. getter/setter functions).

In Object Oriented Programming, the whole point of private members is to prevent any other class from accessing the member. If a derived class was supposed to have access, the member would be protected instead of private.

What are the drawbacks of inheritance?

  1. Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes.
  2. Inherited functions work slower than normal function as there is indirection.
  3. Adds extra memory overload for the compiler as it will have to keep records of the parent as well as the child class.
  4. Often, data members in the base class are left unused which may lead to memory wastage.
  5. Inheritance relationships generally can't be altered at runtime.

Object Oriented Programming Interview Questions  and answers

What is the relationship between a class and an object?

A class is a blueprint which you use to create objects. An object is an instance of a class - it's a concrete 'thing' that you made using a specific class. The word 'instance' indicates the relationship of an object to its class. A class specifies the behaviour of its objects. It is a template which defines the methods (behaviour) and variables (state) to be included in a particular kind of object.

For example, If CAR is a class, Hyundai, Ford, Suzuki are its objects.

Can you call the base class method without creating an instance?

Yes. Its is possible,

  1. If it is a static method.
  2. By inheriting from that class.
  3. From derived classes using the base keyword.

What is hybrid inheritance?

Hybrid inheritance is the inheritance where a class is derived from more than one form or combinations of any inheritance. This means that it is a combination of simple inheritance , multiple inheritance and hierarchical inheritance.

What is Polymorphism?

Generally, Polymorphism is the ability to appear in many forms. Poly means many and Morph means change or form. So polymorphism is the ability to present the same interface for differing underlying forms.

In Object Oriented Programming, polymorphism is when you can treat an object as a generic version of something, but when you access it, the code determines which exact type it is and calls the associated code. This means that when you write multiple implementations of X function where each implementation accepts different parameters types or number of parameters. Based on the type or number of parameter, the compiler (at runtime) decides which implementation of X should be executed when X is called from some code.

More specifically, it is the ability to redefine methods for derived classes . The classic example is the Shape class and all the classes that can inherit from it (square, circle, dodecahedron, irregular polygon, splat and so on). In this example, every class would have its own Draw() function and the client code could simply do:
shape.Draw();

This code is said to be polymorphic, as the exact code which is executed is determined by the sub-class being referenced at runtime.

What are the manipulators in OOP and how it works?

Manipulators are the non-member function specifically designed to be used in conjunction with the insertion (<<) and extraction (>>) operators on stream objects.

They are of two types:

  1. Manipulators with Arguments (for eg: endl)
  2. Manipulators without arguments (for eg: stew(), setfill() )
For example, if you want to print the hexadecimal value of 100 then you can print it as:
cout<<setbase(16)<<100

Difference between Composition and Inheritance?

Composition refers to combining simple types to make more complex ones. An example of Composition in OOPs is where you have an instance of a class within another class, instead of inheriting from it. It simply mean using instance variables that are references to other objects.

  1. Inheritance is a Parent Child Relationship. A class inherits fields and methods from all its superclasses, whether direct or indirect. A subclass can override methods that it inherits, or it can hide fields or methods that it inherits.
  2. Composition means HAS-A relationship while Inheritance means IS-A relationship. For example: A car has wheels, an engine, and seats. Car HAS-A Engine (Composition) and Car IS-A Automobile (Inheritance).

What are the levels of data abstraction?

There are three levels of data abstraction:

  1. Physical (lowest level) : This level describes how data is actually stored in the database.
  2. Logical (middle level) : This level describes what data is stored in the database.
  3. View (Highest level) : This level describes the user interaction with the database system.

What is an Inline function?

Inline function is a function upon which the compiler has been requested to perform inline expansion. It is just a suggestion and the compiler is free to reject it or apply it. Modern compilers will automatically inline some of your smaller methods, however you can always manually inline ones that are bigger.

What is the difference between new and override?

The "new" modifier instructs the compiler to use your child class implementation instead of the parent class implementation. It hides the original method (which doesn't have to be virtual), providing different functionality. This should only be used where it is absolutely necessary. The "override" modifier may be used on virtual methods and must be used on abstract methods. It overrides the functionality of a virtual method in a base class, providing different functionality.

What is Aggregation?

Aggregation is a specialized form of association between two or more objects in which each object has its own life cycle but there exists an ownership as well. Its a directional association , which means it is strictly a one way association. Also, it represents a HAS-A relationship. An essential property of an aggregation relationship is that the whole or parent (i.e. the owner) can exist without the part or child and vice versa.

What is Inversion of Control?

Inversion of Control is a generic term meaning that rather than having the application call the implementations provided by a library (also know as toolkit), a framework calls the implementations provided by the application. It is a pattern used for decoupling components and layers in the system. The pattern is implemented through injecting dependencies into a component when it is constructed. These dependences are usually provided as interfaces for further decoupling and to support testability. So, inversion of control inverts the flow of control of the program. Instead of the callee controlling the flow of control (while creating dependencies), the caller controls the flow of control of the program.

What is Coupling and Cohesion?

A software is consisting of many modules/packages/components and these modules/packages/components consists of elements. Coupling means that interaction/relationship between two modules/packages/components while Cohesion means that interaction between two elements within a modules/packages/components. As for coupling, it refers to how related or dependent two modules/packages/components are toward each other whereas Cohesion refers to what the modules/packages/components can do. For low coupled many modules/packages/components, changing something major in one class should not affect the other. High coupling would make it difficult to change and maintain your code; since classes are closely knit together, making a change could require an entire system revamp. Low cohesion would mean that the class does a great variety of actions - it is broad, unfocused on what it should do. High cohesion means that the class is focused on what it should be doing, i.e. only methods relating to the intention of the class.

High cohesion within many modules/packages/components and low coupling between many modules/packages/components are often regarded as related to high quality in OO programming languages.

Why use getters and setters/accessors?

The point of getters and setters , regardless of language, is to hide the underlying variable. This allows you to add verification logic when attempting to set a value. Also, if the value needs to be changed internally to be represented in a different way, you can do so without breaking any code outside the class. Even if you don't need any verification yet, you might need it in the future.

For example, if you had a field for a birth date, you might only want to allow setting that field to some time in the past. This cannot be enforced if the field is publicly accessible and modifyable. In this case you need the getters and setters. It is good practice to make member variables private so they cannot be modified directly by programs using them.

What is a copy constructor?

A copy constructor is a member function that initializes an object using another object of the same class. It is used to:
  1. Initialize one object from another of the same type.
  2. Copy an object to pass it as an argument to a function.
  3. Copy an object to return it from a function.
You should write your own copy constructor when there is a pointer type variable inside the class. Compiler will insert copy constructor automatically when there is no explicit copy constructor written inside the code.

Is it always necessary to create objects from class?

No. It is possible to call a base class method if it is defined as a static method. An object is necessary to be created if the base class has non-static methods . But if the class has static methods, then objects don't need to be created. You can call the class method directly in this case, using the class name.

How is an abstract class different from an interface?

  1. Interface support multiple implementations while Abstract class does not support multiple inheritance.
  2. Interface does not contain Constructors and Data Member while Abstract class contains Constructors and Data Member.
  3. An interface Contains only incomplete member (signature of member) while An abstract class Contains both incomplete (abstract) and complete member.
  4. Member of interface can not be Static while Complete Member of abstract class can be Static only.

Can you create an instance of an abstract class?

No. You cannot create an instance of an abstract class because it does not have a complete implementation.

What are tokens?

Token is a smallest component in "C" programming language. There are 5 categories of tokens. They are identifiers, constants, strings, keywords and special characters.

What are sealed modifiers?

The sealed modifier prevents other classes from inheriting from it. You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.