OOP - Interview Questions
OOP - Interview Questions
1) Data hiding: Encapsulation is the process of hiding unwanted information, such as restricting
access to any member of an object.
2) Data binding: Encapsulation is the process of binding the data members and the methods
together as a whole, as a class.
4. What is Polymorphism?
Polymorphism is composed of two words - “poly” which means “many”, and “morph” which
means “shapes”. Therefore Polymorphism refers to something that has many shapes.
In OOPs, Polymorphism refers to the process by which some code, data, method, or object
behaves differently under different circumstances or contexts. Compile-time polymorphism and
Run time polymorphism are the two types of polymorphisms in OOPs languages.
5. What is Compile time Polymorphism and how is it different from Runtime Polymorphism?
Compile Time Polymorphism: Compile time polymorphism, also known as Static Polymorphism,
refers to the type of Polymorphism that happens at compile time. What it means is that the
compiler decides what shape or value has to be taken by the entity in the picture. ( method
overloading)
2. Why OOPs?
The main advantage of OOP is better manageable code that covers the following:
1. The overall understanding of the software is increased as the distance between the
language spoken by developers and that spoken by users.
2. Object orientation eases maintenance by the use of encapsulation. One can easily
change the underlying representation by keeping the methods the same.
3. The OOPs paradigm is mainly useful for relatively big software.
3.What is a Class?
A class is a building block of Object Oriented Programs. It is a user-defined data type that
contains the data members and member functions that operate on the data members. It is
like a blueprint or template of objects having common properties and methods.
4. What is an Object?
An object is an instance of a class. Data members and methods of a class cannot be used
directly. We need to create an object (or instance) of the class to use them. In simple
terms, they are the actual world entities that have a state and behavior.
6. What is Encapsulation?
Encapsulation is the binding of data and methods that manipulate them into a single unit such that
the sensitive data is hidden from the users
It is implemented as the processes mentioned below:
1. Data hiding: A language feature to restrict access to members of an object. For
example, private and protected members in C++.
2. Bundling of data and methods together: Data and methods that operate on that data
are bundled together. For example, the data members and member methods that
operate on them are wrapped into a single unit known as a class.
7. What is Abstraction?
Abstraction is similar to data encapsulation and is very important in OOP. It means showing only
the necessary information and hiding the other irrelevant information from the user. Abstraction
is implemented using classes and interfaces.
8. What is Polymorphism?
The word “Polymorphism” means having many forms. It is the property of some code to behave
differently for different contexts. For example, in C++ language, we can define multiple functions
having the same name but different working depending on the context.
Polymorphism can be classified into two types based on the time when the call to the object or
function is resolved. They are as follows:
A) Compile-Time Polymorphism
Compile time polymorphism, also known as static polymorphism or early binding is the type of
polymorphism where the binding of the call to its code is done at the compile time. Method
overloading or operator overloading are examples of compile-time polymorphism.
B) Runtime Polymorphism
Also known as dynamic polymorphism or late binding, runtime polymorphism is the type of
polymorphism where the actual implementation of the function is determined during the runtime
or execution. Method overriding is an example of this method.
The code is easier to maintain and Proper planning is required because OOPs is a little
update. bit tricky.
Fast to implement and easy to redesign The length of the programs is much larger in
Advantages of OOPs Disadvantages of OOPs
13. What is the difference between Structured Programming and Object Oriented Programming?
Structured Programming is a technique that is considered a precursor to OOP and usually consists
of well-structured and separated modules. It is a subset of procedural programming. The
difference between OOPs and Structured Programming is as follows:
Object-Oriented Programming Structural Programming
Modifying and updating the code is Modifying the code is difficult as compared to
easier. OOPs.
14. What are some commonly used Object Oriented Programming Languages?
OOPs paradigm is one of the most popular programming paradigms. It is widely used in many
popular programming languages such as:
C++
Java
Python
Javascript
C#
Ruby
15. What are the different types of Polymorphism?
Polymorphism can be classified into two types based on the time when the call to the object or
function is resolved. They are as follows:
1. Compile Time Polymorphism
2. Runtime Polymorphism
Types of Polymorphism
A) Compile-Time Polymorphism
Compile time polymorphism, also known as static polymorphism or early binding is the
type of polymorphism where the binding of the call to its code is done at the compile
time. Method overloading or operator overloading are examples of compile-time
polymorphism.
B) Runtime Polymorphism
Also known as dynamic polymorphism or late binding, runtime polymorphism is the type
of polymorphism where the actual implementation of the function is determined during
the runtime or execution. Method overriding is an example of this method.
A class that is abstract can have both abstract An interface can only have abstract
and non-abstract methods. methods.
An abstract class can have final, non-final, static The interface has only static and final
Abstract Class Interface
1. Default Constructor
The default constructor is a constructor that doesn’t take any arguments. It is a non-
parameterized constructor that is automatically defined by the compiler when no explicit
constructor definition is provided.
It initializes the data members to their default values.
2. Non-Parameterized Constructor
It is a user-defined constructor having no arguments or parameters.
3. Parameterized Constructor
The constructors that take some arguments are known as parameterized constructors.
4. Copy Constructor
A copy constructor is a member function that initializes an object using another object of
the same class.
In Java, the garbage collector automatically deletes the useless objects so there is no
concept of destructor in Java. We could have used finalize() method as a workaround for
the java destructor but it is also deprecated since Java 9.
27. Can we overload the constructor in a class?
We can overload the constructor in a class. In fact, the default constructor, parameterized
constructor, and copy constructor are the overloaded forms of the constructor.