Most Commonly Asked OOPS Interview Questions
Table of contents
- What is Object-Oriented Programming?
- Why OOP?
- What are main features of OOP?
- What is encapsulation?
- What is Polymorphism? How is it supported by C++?
- What is Inheritance? What is the purpose?
- What is Abstraction?
- What is OOPS?
- What are the advantages of OOPS concepts?
- What is the difference between Procedural programming and OOPS?
- What are the core concepts of OOPS?
- What is Abstraction?
- What is Encapsulation?
- What is the difference between Abstraction and Encapsulation?
- What is Polymorphism?
What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm where the complete software operates as a bunch of objects talking to each other. An object is a collection of data and methods that operate on its data.
Why OOP?
The main advantage of OOP is better manageable code that covers the following.
The overall understanding of the software is increased as the distance between the language spoken by developers and that spoken by users.
Object orientation eases maintenance by the use of encapsulation. One can easily change the underlying representation by keeping the methods the same.
OOP paradigm is mainly useful for relatively big software. See this for a complete example that shows the advantages of OOP over procedural programming.
What are main features of OOP?
Encapsulation
Polymorphism
Inheritance
What is encapsulation?
Encapsulation is referred to as one of the following two notions.
Data hiding: A language feature to restrict access to members of an object. For example, private and protected members in C++.
Bundling of data and methods together: Data and methods that operate on that data are bundled together.
What is Polymorphism? How is it supported by C++?
Polymorphism means that some code or operations or objects behave differently in different contexts. In C++, the following features support polymorphism.
Compile Time Polymorphism : Compile time polymorphism means the compiler knows which function should be called when a polymorphic call is made. C++ supports compiler time polymorphism by supporting features like templates, function overloading, and default arguments.
Run Time Polymorphism : Run time polymorphism is supported by virtual functions. The idea is, that virtual functions are called according to the type of the object pointed or referred to, not according to the type of pointer or reference. In other words, virtual functions are resolved late, at runtime.
What is Inheritance? What is the purpose?
The idea of inheritance is simple, a class is based on another class and uses data and implementation of the other class.
The purpose of inheritance is Code Reuse.
What is Abstraction?
The first thing with which one is confronted when writing programs is the problem. Typically we are confronted with “real-life” problems and we want to make life easier by providing a program for the problem. However, real-life problems are nebulous and the first thing we have to do is to try to understand the problem to separate necessary from unnecessary details: We try to obtain our own abstract view, or model, of the problem. This process of modeling is called abstraction.
What is OOPS?
Object-Oriented Programming System is the programming technique to write programs based on real-world objects. The states and behaviors of an object are represented as the member variables and methods. In OOPS programming programs are organized around objects and data rather than actions and logic.
What are the advantages of OOPS concepts?
Major advantages of OOPS programming are;
Simplicity : OOPS programming objects model real world objects, so the complexity is reduced and the program structure is clear.
Modularity : Each object forms a separate entity whose internal workings are decoupled from other parts of the system.
Modifiability : It is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods.
Extensibility : Adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.
Maintainability : Objects can be maintained separately, making locating and fixing problems easier.
Reusability : Objects can be reused in different programs.
What is the difference between Procedural programming and OOPS?
Procedural language is based on functions but object-oriented language is based on real world objects.
Procedural language gives importance on the sequence of function execution but object oriented language gives importance on states and behaviors of the objects.
Procedural language exposes the data to the entire program but object-oriented language encapsulates the data.
Procedural language follows top-down programming paradigm but object oriented language follows bottom up programming paradigm.
Procedural language is complex in nature so it is difficult to modify, extend and maintain but object oriented language is less complex in nature so it is easier to modify, extend and maintain.
Procedural language provides less scope of code reuse but object oriented language provides more scope of code reuse.
What are the core concepts of OOPS?
OOPS core concepts are;
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
- Composition
- Association
- Aggregation
What is Abstraction?
Abstraction is an OOPS concept to construct the structure of the real world objects. During this construction only the general states and behaviors are taken and more specific states and behaviors are left aside for the implementers.
What is Encapsulation?
Encapsulation is an OOPS concept to create and define the permissions and restrictions of an object and its member variables and methods. A very simple example to explain the concept is to make the member variables of a class private and providing public getter and setter methods. Java provides four types of access level modifiers: public, protected, no modifier and private.
What is the difference between Abstraction and Encapsulation?
“Program to interfaces, not implementations” is the principle for Abstraction and “Encapsulate what varies” is the OO principle for Encapsulation.
Abstraction provides a general structure of a class and leaves the details for the implementers. Encapsulation is to create and define the permissions and restrictions of an object and its member variables and methods.
Abstraction is implemented in Java using interface and abstract class while Encapsulation is implemented using four types of access level modifiers: public, protected, no modifier and private.
What is Polymorphism?
Polymorphism is the occurrence of something in various forms. Java supports various forms of polymorphism like polymorphic reference variables, polymorphic method, polymorphic return types and polymorphic argument types.