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: Foo HAS-A 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?
- Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes.
- Inherited functions work slower than normal function as there is indirection.
- Adds extra memory overload for the compiler as it will have to keep records of the parent as well as the child class.
- Often, data members in the base class are left unused which may lead to memory wastage.
- Inheritance relationships generally can't be altered at runtime.
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,
- If it is a static method.
- By inheriting from that class.
- 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:
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:
- Manipulators with Arguments (for eg: endl)
- 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: