OOPs Interview Questions
Object-oriented programming (OOPs) is a programming paradigm that is based on the concept of objects rather than just functions and procedures. It is the most popular methodology among developers.
Nowadays tech giants demanding and hiring who has expertise in object-oriented approaches and patterns and conducting interviews for the same. The advantage of hiring such candidates is that they can also learn other OOP languages easily as per organization requirements. Since, going through the section, you can increase your chance to get hire by companies if you have well prepared for OOPs interview questions.
In this section, we have collected some commonly asked OOPs interview questions for both fresher and experienced. It can help you to crack the interview to get your dream job.
1) What do you understand by OOP?
OOP stands for object-oriented programming. It is a programming paradigm that revolves around the object rather than function and procedure. In other words, it is an approach for developing applications that emphasize on objects. An object is a real word entity that contains data and code. It allows binding data and code together.
2) Name any seven widely used OOP languages.
There are various OOP languages but the most widely used are:
3) What is the purpose of using OOPs concepts?
The aim of OOP is to implement real-world entities like inheritance, hiding, polymorphism in programming. The main purpose of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.
4) What are the four main features of OOPs?
The OOP has the following four features:
5) Why OOP is 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.
6) What are the advantages and disadvantages of OOP?
Advantages of OOP
Disadvantages of OOP
7) What are the limitations of OOPs?
8) What are the differences between object-oriented programming and structural programming?
9) What do you understand by pure object-oriented language? Why Java is not a pure object-oriented programming language?
The programming language is called pure object-oriented language that treats everything inside the program as an object. The primitive types are not supported by the pure OOPs language. There are some other features that must satisfy by a pure object-oriented language:
Java is not a pure object-oriented programming language because pre-defined data types in Java are not treated as objects. Hence, it is not an object-oriented language.
10) What do you understand by class and object? Also, give example.
Class: A class is a blueprint or template of an object. It is a user-defined data type. Inside a class, we define variables, constants, member functions, and other functionality. It does not consume memory at run time. Note that classes are not considered as a data structure. It is a logical entity. It is the best example of data binding.
Object: An object is a real-world entity that has attributes, behavior, and properties. It is referred to as an instance of the class. It contains member functions, variables that we have defined in the class. It occupies space in the memory. Different objects have different states or attributes, and behaviors.
The following figure best illustrates the class and object.
11) What are the differences between class and object?
12) What are the key differences between class and structure?
13) What is the concept of access specifiers when should we use these?
In OOPs language, access specifiers are reserved keyword that is used to set the accessibility of the classes, methods and other members of the class. It is also known as access modifiers. It includes public, private, and protected. There is some other access specifier that is language-specific. Such as Java has another access specifier default. These access specifiers play a vital role in achieving one of the major functions of OOP, i.e. encapsulation. The following table depicts the accessibility.
14) What are the manipulators in OOP and how it works?
Manipulators are helping functions. It is used to manipulate or modify the input or output stream. The modification is possible by using the insertion (<<) and extraction (>>) operators. Note that the modification of input or output stream does not mean to change the values of variables. There are two types of manipulators with arguments or without arguments.
The example of manipulators that do not have arguments is endl, ws, flush, etc. Manipulators with arguments are setw(val), setfill(c), setbase(val), setiosflags(flag). Some other manipulators are showpos, fixed, scientific, hex, dec, oct, etc.
15) What are the rules for creating a constructor?
16) What are the differences between the constructor and the method in Java?
17) How does procedural programming be different from OOP differ?
18) What are the differences between error and exception?
19) What are the characteristics of an abstract class?
An abstract class is a class that is declared as abstract. It cannot be instantiated and is always used as a base class. The characteristics of an abstract class are as follows:
The purpose of an abstract class is to provide a common definition of the base class that multiple derived classes can share.
20) Is it possible for a class to inherit the constructor of its base class?
No, a class cannot inherit the constructor of its base class.
21) Identify which OOPs concept should be used in the following scenario?
A group of 5 friends, one boy never gives any contribution when the group goes for the outing. Suddenly a beautiful girl joins the same group. The boy who never contributes is now spending a lot of money for the group.
22) What is composition?
Composition is one of the vital concepts in OOP. It describes a class that references one or more objects of other classes in instance variables. It allows us to model a has-a association between objects. We can find such relationships in the real world. For example, a car has an engine. the following figure depicts the same
The main benefits of composition are:
23) What are the differences between copy constructor and assignment operator?
The copy constructor and the assignment operator (=) both are used to initialize one object using another object. The main difference between the two is that the copy constructor allocates separate memory to both objects i.e. existing object and newly created object while the assignment operator does not allocate new memory for the newly created object. It uses the reference variable that points to the previous memory block (where an old object is located).
Syntax of Copy Constructor
Syntax of Assignment Operator
24) What is the difference between Composition and Inheritance?
Inheritance means an object inheriting reusable properties of the base class. Compositions mean that an object holds other objects. In Inheritance, there is only one object in memory (derived object) whereas, in Composition, the parent object holds references of all composed objects. From a design perspective, inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects.
25) What is constructor chaining?
In OOPs, constructor chaining is a sequence of invoking constructors (of the same class) upon initializing an object. It is used when we want to invoke a number of constructors, one after another by using only an instance. In other words, if a class has more than one constructor (overloaded) and one of them tries to invoke another constructor, this process is known as constructor chaining. In C++, it is known as constructor delegation and it is present from C++ 11.
26) What are the limitations of inheritance?
27) What are the differences between Inheritance and Polymorphism?
28) What is Coupling in OOP and why it is helpful?
In programming, separation of concerns is known as coupling. It means that an object cannot directly change or modify the state or behavior of other objects. It defines how closely two objects are connected together. There are two types of coupling, loose coupling, and tight coupling.
Objects that are independent of one another and do not directly modify the state of other objects is called loosely coupled. Loose coupling makes the code more flexible, changeable, and easier to work with.
Objects that depend on other objects and can modify the states of other objects are called tightly coupled. It creates conditions where modifying the code of one object also requires changing the code of other objects. The reuse of code is difficult in tight coupling because we cannot separate the code.
Since using loose coupling is always a good habit.
29) Name the operators that cannot be overload.
30) What is the difference between new and override?
The new modifier instructs the compiler to use the new implementation instead of the base class function. Whereas, Override modifier helps to override the base class function.
virtual: indicates that a method may be overridden by an inheritor
override: Overrides the functionality of a virtual method in a base class, providing different functionality.
new: Hides the original method (which doesn't have to be virtual), providing different functionality. This should only be used where it is absolutely necessary.
When you hide a method, you can still access the original method by upcasting to the base class. This is useful in some scenarios, but dangerous.
31) Explain overloading and overriding with example?
Overloading is a concept in OOP when two or more methods in a class with the same name but the method signature is different. It is also known as compile-time polymorphism. For example, in the following code snippet, the method add() is an overloaded method.
If a method with the same method signature is presented in both child and parent class is known as method overriding. The methods must have the same number of parameters and the same type of parameter. It overrides the value of the parent class method. It is also known as runtime polymorphism. For example, consider the following program.
32) What is Cohesion in OOP?
In OOP, cohesion refers to the degree to which the elements inside a module belong together. It measures the strength of the relationship between the module and data. In short, cohesion represents the clarity of the responsibilities of a module. It is often contrasted with coupling.
It focuses on a how single module or class is intended. Higher the cohesiveness of the module or class, better is the object-oriented design.
There are two types of cohesion, i.e. High and Low.
High cohesion often associates with loose coupling and vice versa.
33) Give a real-world example of polymorphism?
The general meaning of Polymorphism is one that has different forms. The best real-world example of polymorphism is a person that plays different roles at different palaces or situations.
Hence, the same person possesses different behavior in different situations. It is called polymorphism.
34) What is the difference between a base class and a superclass?
The base class is the root class- the most generalized class. At the same time, the superclass is the immediate parent class from which the other class inherits.
35) What is data abstraction and how can we achieve data abstraction?
It is one of the most important features of OOP. It allows us to show only essential data or information to the user and hides the implementation details from the user. A real-world example of abstraction is driving a car. When we drive a car, we do not need to know how the engine works (implementation) we only know how ECG works.
There are two ways to achieve data abstraction
36) What are the levels of data abstraction?
There are three levels of data abstraction:
37) What are the types of variables in OOP?
There are three types of variables:
Instance Variable: It is an object-level variable. It should be declared inside a class but must be outside a method, block, and constructor. It is created when an object is created by using the new keyword. It can be accessed directly by calling the variable name inside the class.
Static Variable: It is a class-level variable. It is declared with keyword static inside a class but must be outside of the method, block, and constructor. It stores in static memory. Its visibility is the same as the instance variable. The default value of a static variable is the same as the instance variable. It can be accessed by calling the class_name.variable_name.
Local Variable: It is a method-level variable. It can be declared in method, constructor, or block. Note that the use of an access modifier is not allowed with local variables. It is visible only to the method, block, and constructor in which it is declared. Internally, it is implemented at the stack level. It must be declared and initialized before use.
Another type of variable is used in object-oriented programming is the reference variable.
Reference Variable: It is a variable that points to an object of the class. It points to the location of the object that is stored in the memory.
38) Is it possible to overload a constructor?
Yes, the constructors can be overloaded by changing the number of arguments accepted by the constructor or by changing the data type of the parameters. For example:
39) Can we overload the main() method in Java also give an example?
Yes, we can also overload the main() method in Java. Any number of main() methods can be defined in the class, but the method signature must be different. Consider the following code.
40) Consider the following scenario:
If a class Demo has a static block and a main() method. A print statement is presented in both. The question is which one will first execute, static block or the main() method, and why?
JVM first executes the static block on a priority basis. It means JVM first goes to static block even before it looks for the main() method in the program. After that main() method will be executed.