0% found this document useful (0 votes)
21 views13 pages

OOP - Interview Questions

OOP wuestions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views13 pages

OOP - Interview Questions

OOP wuestions
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

1. What is meant by the term OOPs?

OOPs refers to Object-Oriented Programming. It is the programming paradigm that is defined


using objects. Objects can be considered as real-world instances of entities like class, that have
some characteristics and behaviors.
2. What is the need for OOPs?
There are many reasons why OOPs is mostly preferred, but the most important among them are:
 OOPs helps users to understand the software easily, although they don’t know the actual
implementation.
 With OOPs, the readability, understandability, and maintainability of the code increase
multifold.
 Even very big software can be easily written and managed easily using OOPs.
3. What are some major Object Oriented Programming languages?
The programming languages that use and follow the Object-Oriented Programming paradigm or
OOPs, are known as Object-Oriented Programming languages. Some of the major Object-Oriented
Programming languages include:
 Java
 C++
 Javascript
 Python
 PHP
4. What are some other programming paradigms other than OOPs?
Programming paradigms refers to the method of classification of programming languages based
on their features. There are mainly two types of Programming Paradigms:
 Imperative Programming Paradigm
 Declarative Programming Paradigm
Now, these paradigms can be further classified based:

1. Imperative Programming Paradigm: Imperative programming focuses on HOW to execute


program logic and defines control flow as statements that change a program state. This can be
further classified as:
a) Procedural Programming Paradigm: Procedural programming specifies the steps a program
must take to reach the desired state, usually read in order from top to bottom.
b) Object-Oriented Programming or OOP : Object-oriented programming (OOP) organizes programs
as objects, that contain some data and have some behavior.
c) Parallel Programming: Parallel programming paradigm breaks a task into subtasks and focuses
on executing them simultaneously at the same time.

2. Declarative Programming Paradigm: Declarative programming focuses on WHAT to execute


and defines program logic, but not a detailed control flow. Declarative paradigm can be further
classified into:
a) Logical Programming Paradigm : Logical programming paradigm is based on formal logic, which
refers to a set of sentences expressing facts and rules about how to solve a problem
b) Functional Programming Paradigm : Functional programming is a programming paradigm where
programs are constructed by applying and composing functions.
c) Database Programming Paradigm : Database programming model is used to manage data and
information structured as fields, records, and files.

5. What is meant by Structured Programming?


Structured Programming refers to the method of programming which consists of a completely
structured control flow. Here structure refers to a block, which contains a set of rules, and has a
definitive control flow, such as (if/then/else), (while and for), block structures, and subroutines.
Nearly all programming paradigms include Structured programming, including the OOPs model.
6. What are the main features of OOPs?
OOPs or Object Oriented Programming mainly comprises of the below four features, and make
sure you don't miss any of these:
 Inheritance
 Encapsulation
 Polymorphism
 Data Abstraction

7. What are some advantages of using OOPs?


 OOPs is very helpful in solving very complex level of problems.
 Highly complex programs can be created, handled, and maintained easily using object-
oriented programming.
 OOPs, promote code reuse, thereby reducing redundancy.
 OOPs also helps to hide the unnecessary details with the help of Data Abstraction.
 OOPs, are based on a bottom-up approach, unlike the Structural programming paradigm,
which uses a top-down approach.
 Polymorphism offers a lot of flexibility in OOPs.
8. Why is OOPs so popular?
OOPs programming paradigm is considered as a better style of programming. Not only it helps in
writing a complex piece of code easily, but it also allows users to handle and maintain them easily
as well. Not only that, the main pillar of OOPs - Data Abstraction, Encapsulation, Inheritance, and
Polymorphism, makes it easy for programmers to solve complex scenarios. As a result of these,
OOPs is so popular.
Advanced OOPs Interview Questions
1. What is a class?
A class can be understood as a template or a blueprint, which contains some values, known as
member data or member, and some set of rules, known as behaviors or functions. So when an
object is created, it automatically takes the data and functions that are defined in the class.
Therefore the class is basically a template or blueprint for objects. Also one can create as many
objects as they want based on a class.
For example, first, a car’s template is created. Then multiple units of car are created based on that
template.
2. What is an object?
An object refers to the instance of the class, which contains the instance of the members and
behaviors defined in the class template. In the real world, an object is an actual entity to which a
user interacts, whereas class is just the blueprint for that object. So the objects consume space
and have some characteristic behavior.
For example, a specific car.
One can visualize Encapsulation as the method of putting everything that is required to do the job,
inside a capsule and presenting that capsule to the user. What it means is that by Encapsulation,
all the necessary data and methods are bind together and all the unnecessary details are hidden to
the normal user. So Encapsulation is the process of binding data members and methods of a
program together to do a specific job, without revealing unnecessary details.
Encapsulation can also be defined in two different ways:

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)

Runtime Polymorphism: Runtime polymorphism, also known as Dynamic Polymorphism, refers to


the type of Polymorphism that happens at the run time. What it means is it can't be decided by
the compiler. Therefore what shape or value has to be taken depends upon the execution. Hence
the name Runtime Polymorphism. (method overriding)

7. What is meant by Inheritance?


The term “inheritance” means “receiving some quality or behavior from a parent to an offspring.”
In object-oriented programming, inheritance is the mechanism by which an object or class
(referred to as a child) is created using the definition of another object or class (referred to as a
parent). Inheritance not only helps to keep the implementation simpler but also helps to facilitate
code reuse.
8. What is Abstraction?
If you are a user, and you have a problem statement, you don't want to know how the
components of the software work, or how it's made. You only want to know how the software
solves your problem. Abstraction is the method of hiding unnecessary details from the necessary
ones. It is one of the main features of OOPs.
For example, consider a car. You only need to know how to run a car, and not how the wires are
connected inside it. This is obtained using Abstraction.
9. How much memory does a class occupy?
Classes do not consume any memory. They are just a blueprint based on which objects are
created. Now when objects are created, they actually initialize the class members and methods
and therefore consume memory.
10. Is it always necessary to create objects from class?
No. 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.
11. What is a constructor?
Constructors are special methods whose name is the same as the class name. The constructors
serve the special purpose of initializing the objects.
For example, suppose there is a class with the name “MyClass”, then when you instantiate this
class, you pass the syntax:
MyClass myClassObject = new MyClass();
Now here, the method called after “new” keyword - MyClass(), is the constructor of this class. This
will help to instantiate the member data and methods and assign them to the object
myClassObject.
12. What is a subclass?
The subclass is a part of Inheritance. The subclass is an entity, which inherits from another class. It
is also known as the child class.
13. Define a superclass?
Superclass is also a part of Inheritance. The superclass is an entity, which allows subclasses or child
classes to inherit from itself.

14. What is an interface?


An interface refers to a special type of class, which contains methods, but not their definition.
Only the declaration of methods is allowed inside an interface. To use an interface, you cannot
create objects. Instead, you need to implement that interface and define the methods for their
implementation.
15. What is meant by static polymorphism?
Static Polymorphism is commonly known as the Compile time polymorphism. Static polymorphism
is the feature by which an object is linked with the respective function or operator based on the
values during the compile time. Static or Compile time Polymorphism can be achieved through
Method overloading or operator overloading.
16. What is meant by dynamic polymorphism?
Dynamic Polymorphism or Runtime polymorphism refers to the type of Polymorphism in OOPs, by
which the actual implementation of the function is decided during the runtime or execution. The
dynamic or runtime polymorphism can be achieved with the help of method overriding.
17. What is the difference between overloading and overriding?
Overloading is a compile-time polymorphism feature in which an entity has multiple
implementations with the same name. For example, Method overloading and Operator
overloading.
Whereas Overriding is a runtime polymorphism feature in which an entity has the same name, but
its implementation changes during execution. For example, Method overriding.
Image
18. How is data abstraction accomplished?
Data abstraction is accomplished with the help of abstract methods or abstract classes.
19. What is an abstract class?
An abstract class is a special class containing abstract methods. The significance of abstract class is
that the abstract methods inside it are not implemented and only declared. So as a result, when a
subclass inherits the abstract class and needs to use its abstract methods, they need to define and
implement them.
20. How is an abstract class different from an interface?
Interface and abstract classes both are special types of classes that contain only the methods
declaration and not their implementation. But the interface is entirely different from an abstract
class. The main difference between the two is that when an interface is implemented, the subclass
must define all its methods and provide its implementation. Whereas in object-oriented
programming, when a subclass inherits from an abstract class with abstract methods, the subclass
is generally required to provide concrete implementations for all of those abstract methods in the
abstract class unless the subclass itself is declared as abstract.
Also, an abstract class can contain abstract methods as well as non-abstract methods.
21. What are access specifiers and what is their significance?
Access specifiers, as the name suggests, are a special type of keywords, which are used to control
or specify the accessibility of entities like classes, methods, etc. Some of the access specifiers or
access modifiers include “private”, “public”, etc. These access specifiers also play a very vital role
in achieving Encapsulation - one of the major features of OOPs.
22. What is an exception?
An exception can be considered as a special event, which is raised during the execution of a
program at runtime, that brings the execution to a halt. The reason for the exception is mainly due
to a position in the program, where the user wants to do something for which the program is not
specified, like undesirable input.
23. What is meant by exception handling?
No one wants its software to fail or crash. Exceptions are the major reason for software failure.
The exceptions can be handled in the program beforehand and prevent the execution from
stopping. This is known as exception handling.
So exception handling is the mechanism for identifying the undesirable states that the program
can reach and specifying the desirable outcomes of such states.
Try-catch is the most common method used for handling exceptions in the program.
24. What is meant by Garbage Collection in OOPs world?
Object-oriented programming revolves around entities like objects. Each object consumes
memory and there can be multiple objects of a class. So if these objects and their memories are
not handled properly, then it might lead to certain memory-related errors and the system might
fail.
Garbage collection refers to this mechanism of handling the memory in the program. Through
garbage collection, the unwanted memory is freed up by removing the objects that are no longer
needed.
25. Can we run a Java application without implementing the OOPs concept?
No. Java applications are based on Object-oriented programming models or OOPs concept, and
hence they cannot be implemented without it.

FROM GEEKS FOR GEEKS


OOPs Interview Questions

1. What is Object Oriented Programming (OOPs)?


Object Oriented Programming (also known as OOPs) 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 the methods which operate on that data.

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.

5. What are the main features of OOPs?


The main feature of the OOPs, also known as 4 pillars or basic principles of OOPs are as follows:
1. Encapsulation
2. Data Abstraction
3. Polymorphism
4. Inheritance

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:

 Compile Time Polymorphism


 Runtime 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.

9. What is Inheritance? What is its purpose?


The idea of inheritance is simple, a class is derived from another class and uses data and
implementation of that other class. The class which is derived is called child or derived or subclass
and the class from which the child class is derived is called parent or base or superclass.
The main purpose of Inheritance is to increase code reusability. It is also used to achieve Runtime
Polymorphism.

10. What are access specifiers? What is their significance in OOPs?


Access specifiers are special types of keywords that are used to specify or control the accessibility
of entities like classes, methods, and so on. Private, Public, and Protected are examples of access
specifiers or access modifiers.
The key components of OOPs, encapsulation and data hiding, are largely achieved because of
these access specifiers.

11. What are the advantages and disadvantages of OOPs?


Advantages of OOPs Disadvantages of OOPs

The programmer should be well-skilled and should


OOPs provides enhanced code
have excellent thinking in terms of objects as
reusability.
everything is treated as an object in OOPs.

The code is easier to maintain and Proper planning is required because OOPs is a little
update. bit tricky.

It provides better data security by


OOPs concept is not suitable for all kinds of
restricting data access and avoiding
problems.
unnecessary exposure.

Fast to implement and easy to redesign The length of the programs is much larger in
Advantages of OOPs Disadvantages of OOPs

resulting in minimizing the complexity


comparison to the procedural approach.
of an overall program.

12.What other paradigms of programming exist besides OOPs?


The programming paradigm is referred to the technique or approach of writing a program. The
programming paradigms can be classified into the following types:

1. Imperative Programming Paradigm


It is a programming paradigm that works by changing the program state through assignment
statements. The main focus in this paradigm is on how to achieve the goal. The following
programming paradigms come under this category:
1. Procedural Programming Paradigm: This programming paradigm is based on the
procedure call concept. Procedures, also known as routines or functions are the basic
building blocks of a program in this paradigm.
2. Object-Oriented Programming or OOP: In this paradigm, we visualize every entity as
an object and try to structure the program based on the state and behavior of that
object.
3. Parallel Programming: The parallel programming paradigm is the processing of
instructions by dividing them into multiple smaller parts and executing them
concurrently.
2. Declarative Programming Paradigm
Declarative programming focuses on what is to be executed rather than how it should be
executed. In this paradigm, we express the logic of a computation without considering its control
flow. The declarative paradigm can be further classified into:
1. Logical Programming Paradigm: It is based on formal logic where the program
statements express the facts and rules about the problem in the logical form.
2. Functional Programming Paradigm: Programs are created by applying and composing
functions in this paradigm.
3. Database Programming Paradigm: To manage data and information organized as
fields, records, and files, database programming models are utilized.

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

Programming that is object-oriented is A program’s logical structure is provided by


built on objects having a state and structural programming, which divides programs
behavior. into their corresponding functions.

It follows a bottom-to-top approach. It follows a Top-to-Down approach.

Restricts the open flow of data to


No restriction to the flow of data. Anyone can
authorized parts only providing better
access the data.
data security.

Enhanced code reusability due to the


Code reusability is achieved by using functions and
concepts of polymorphism and
loops.
inheritance.

In this, methods are written globally and


In this, the method works dynamically, making
code lines are processed one by one i.e.,
calls as per the need of code for a certain time.
Run sequentially.

Modifying and updating the code is Modifying the code is difficult as compared to
easier. OOPs.

Data is given more importance in OOPs. Code is given more importance.

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.

16. What is the difference between overloading and overriding?


A compile-time polymorphism feature called overloading allows an entity to have
numerous implementations of the same name. Method overloading and operator
overloading are two examples.
Overriding is a form of runtime polymorphism where an entity with the same name but a
different implementation is executed. It is implemented with the help of virtual functions.

17. Are there any limitations on Inheritance?


Yes, there are more challenges when you have more authority. Although inheritance is a very
strong OOPs feature, it also has significant drawbacks.
 As it must pass through several classes to be implemented, inheritance takes longer to
process.
 The base class and the child class, which are both engaged in inheritance, are also
closely related to one another (called tightly coupled). Therefore, if changes need to be
made, they may need to be made in both classes at the same time.
 Implementing inheritance might be difficult as well. Therefore, if not implemented
correctly, this could result in unforeseen mistakes or inaccurate outputs.

18. What different types of inheritance are there?


Inheritance can be classified into 5 types which are as follows:
1. Single Inheritance: Child class derived directly from the base class
2. Multiple Inheritance: Child class derived from multiple base classes.
3. Multilevel Inheritance: Child class derived from the class which is also derived from
another base class.
4. Hierarchical Inheritance: Multiple child classes derived from a single base class.
5. Hybrid Inheritance: Inheritance consisting of multiple inheritance types of the above
specified.

19. What is an interface?


A unique class type known as an interface contains methods but not their definitions.
Inside an interface, only method declaration is permitted. You cannot make objects using
an interface. Instead, you must put that interface into use and specify the procedures for
doing so.

20. How is an abstract class different from an interface?


Both abstract classes and interfaces are special types of classes that just include the
declaration of the methods, not their implementation. An abstract class is completely
distinct from an interface, though. Following are some major differences between an
abstract class and an interface.
Abstract Class Interface

When an abstract class is inherited, however, When an interface is implemented, the


the subclass is not required to supply the subclass is required to specify all of the
definition of the abstract method until and interface’s methods as well as their
unless the subclass actually uses it. implementation.

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

and non-static variables. variables.

Abstract class doesn’t support multiple


An interface supports multiple inheritance.
inheritance.

21. How much memory does a class occupy?


Classes do not use memory. They merely serve as a template from which items are made.
Now, objects actually initialize the class members and methods when they are created, using
memory in the process.

22. Is it always necessary to create objects from class?


No. If the base class includes non-static methods, an object must be constructed. But no
objects need to be generated if the class includes static methods. In this instance, you can use the
class name to directly call those static methods.

23. What is the difference between a structure and a class in C++?


The structure is also a user-defined datatype in C++ similar to the class with the following
differences:
 The major difference between a structure and a class is that in a structure, the
members are set to public by default while in a class, members are private by default.
 The other difference is that we use struct for declaring structure and class for declaring
a class in C++.

24. What is Constructor?


A constructor is a block of code that initializes the newly created object. A constructor
resembles an instance method but it’s not a method as it doesn’t have a return type. It
generally is the method having the same name as the class but in some languages, it might
differ.

25. What are the various types of constructors in C++?


The most common classification of constructors includes:
1. Default Constructor
2. Non-Parameterized Constructor
3. Parameterized Constructor
4. Copy Constructor

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.

26. What is a destructor?


A destructor is a method that is automatically called when the object is made of scope or
destroyed.
In C++, the destructor name is also the same as the class name but with the (~) tilde
symbol as the prefix.

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.

28. Can we overload the destructor in a class?


No. A destructor cannot be overloaded in a class. The can only be one destructor present
in a class.

29. What is the virtual function?


A virtual function is a function that is used to override a method of the parent class in the
derived class. It is used to provide abstraction in a class.
In C++, a virtual function is declared using the virtual keyword,
In Java, every public, non-static, and non-final method is a virtual function.

30. What is pure virtual function?


A pure virtual function, also known as an abstract function is a member function that
doesn’t contain any statements. This function is defined in the derived class if needed.

31. What is an abstract class?


In general terms, an abstract class is a class that is intended to be used for inheritance. It
cannot be instantiated. An abstract class can consist of both abstract and non-abstract
methods.
In C++, an abstract class is a class that contains at least one pure virtual function.
In Java, an abstract class is declared with an abstract keyword.

You might also like