OOPS Interview Cheatsheet
OOPS Interview Cheatsheet
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 behaviours.
2. Why OOP?
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 codebases 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
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.
5. What are the main features of OOPs? [American Express] [Microsoft] [Intuit] [Oracle]
OOPs or Object Oriented Programming mainly comprises of the below four features:
● Inheritance
● Encapsulation
● Polymorphism
● Data Abstraction
● 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. What is a class?
A class is a template or a blueprint, which contains some values, known as member data or member, and some set of
rules, known as behaviours or functions. So when an object is created, it automatically takes the data and functions
that are defined in the class.
9. What is an object?
An object refers to the instance of the class, which contains the instance of the members and behaviours 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 behaviour.
10. What is encapsulation?
Encapsulation is 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.
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.
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: 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.
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.
● Compile Time Polymorphism: C++ supports compile-time polymorphism with the help of features like templates,
function overloading, and default arguments.
● Runtime Polymorphism: C++ supports Runtime polymorphism with the help of features like virtual functions.
Virtual functions take the shape of the functions based on the type of object in reference and are resolved at
runtime.
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.
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.
16. What are the levels of data abstraction?
To make the system efficient for retrieval of data and reduce the complexity of the users, developers use the method of
Data Abstraction.
There are mainly three levels of data abstraction:
Classes do not consume any memory. They are just a blueprint based on which objects are created. Now when objects
are created, they actually initialise the class members and methods and therefore consume memory.
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.
Constructors are special methods whose name is the same as the class name. The constructors serve the special
purpose of initialising the objects.
For example, suppose there is a class with the name “MyClass”, then when you instantiate this class, you pass the
syntax:
● Default constructor: The default constructor is the constructor which doesn’t take any argument. It has no
parameters.
class ABC
{
int x;
ABC()
{
x = 0;
}
}
● Parameterised constructor: The constructors that take some arguments are known as parameterised constructors.
class ABC
{
int x;
ABC(int y)
{
x = y;
}
}
● Copy constructor: A copy constructor is a member function that initialises an object using another object of the
same class.
class ABC
{
int x;
ABC(int y)
{
x = y;
}
// Copy constructor
ABC(ABC abc)
{
x = abc.x;
}
}
Copy Constructor is a type of constructor, whose purpose is to copy an object to another. What it means is that a copy
constructor will clone an object and its values, into another object, is provided that both the objects are of the same
class.
Contrary to constructors, which initialise objects and specify space for them, Destructors are also special methods. But
destructors free up the resources and memory occupied by an object. Destructors are automatically called when an
object is being destroyed.
23. Are class and structure the same? If not, what's the difference between a class and a structure?
No, class and structure are not the same. Though they appear to be similar, they have differences that make them
apart. For example, the structure is saved in the stack memory, whereas the class is saved in the heap memory. Also,
Data Abstraction cannot be achieved with the help of structure, but with class, Abstraction is majorly used.
Inheritance is one of the major features of object-oriented programming, by which an entity inherits some
characteristics and behaviours of some other entity and makes them their own. Inheritance helps to improve and
facilitate code reuse.
Let me explain to you with a common example. Let's take three different vehicles - a car, truck, or bus. These three are
entirely different from one another with their own specific characteristics and behavior. But, in all three, you will find
some common elements, like steering wheel, accelerator, clutch, brakes, etc. Though these elements are used in
different vehicles, still they have their own features which are common among all vehicles. This is achieved with
inheritance. The car, the truck, and the bus have all inherited the features like steering wheel, accelerator, clutch,
brakes, etc, and used them as their own. Due to this, they did not have to create these components from scratch,
thereby facilitating code reuse.
26. What are the various types of inheritance? [American Express] [Microsoft]
● Single inheritance
● Multiple inheritances
● Multi-level inheritance
● Hierarchical inheritance
● Hybrid inheritance
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.
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.
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.
Function overloading is a feature of object oriented programming where two or more functions can have the same
name but different parameters.
#include <bits/stdc++.h>
using namespace std;
void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(string s) {
cout << " Here is string " << s << endl;
}
int main() {
print(10);
print(10.10);
print("ten");
return 0;
}
31. What is the difference between overloading and overriding? [American Express] [Adobe] [American
Express] [Goldman Sachs] [Oracle]
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.
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.
Interface and abstract class 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 when an abstract class is inherited, the subclass does not need to provide the definition of its abstract
method, until and unless the subclass is using it.
A friend function of a class is defined outside that class' scope but it has the right to access all private and protected
members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not
member functions.
A friend can be a function, function template, or member function, or a class or class template, in which case the entire
class and all of its members are friends.
class Box {
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
return 0;
}
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.
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.
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.
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.
39. Can we run a Java application without implementing the OOPs concept?
No, Java applications are based on object-oriented programming models, and hence they cannot be implemented
without it.
40. Can we run a C++ application without implementing the OOPs concept?
Yes, C++ can be implemented without OOPs, as it supports the C-like structural programming model.
41. What is a virtual function? [American Express] [Goldman Sachs] [Adobe] [Microsoft]
A virtual function is a member function which is declared within a base class and is re-defined(overriden) by a derived
class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual
function for that object and execute the derived class’s version of the function.
A pure virtual function (or abstract function) is a virtual function for which we can have implementation, but we must
override that function in the derived class, otherwise the derived class will also become abstract class.
If you make any variable as final, you cannot change the value of final variable, i.e., it will be constant.
Garbage means unreferenced objects and Garbage Collection is a process of reclaiming the runtime unused memory
automatically. In other words, it is a way to destroy the unused objects. To do so, we use free() function in C language,
delete() in C++, but in Java, it is performed automatically.
Composition is a way to design or implement the "has-a" relationship. The composition relationship of two objects is
possible when one object contains another object, and that object is fully dependent on it. The contained object
should not exist without the existence of its parent object.
In a simple way, we can say it is a technique through which we can describe the reference between two or more
classes. And for that, we use the instance variable, which should be created before it is used.
The copy assignment operator lets you create a new object from an existing one by initialisation. A copy assignment
operator of a class A is a non-static non-template member function that has one of the following forms:
● A::operator=(A)
● A::operator=(A&)
● A::operator=(const A&)
● A::operator=(volatile A&)
● A::operator=(const volatile A&)
47. Name the operators that cannot be overloaded.
Manipulators are helping functions that can modify the input/output stream. It does not mean that we change the
value of a variable, it only modifies the I/O stream using insertion (<<) and extraction (>>) operators. For example, if
we want to print the hexadecimal value of 100 then we can print it as:
cout<<setbase(16)<<100;
49. What is const keyword?
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from
modifying it.
The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be
overridden in a derived class.
If a class has a constructor which can be called with a single argument, then this constructor becomes conversion
constructor because such a constructor allows conversion of the single argument to the class being constructed.
We can avoid such implicit conversions as these may lead to unexpected results. We can make the constructor explicit with
the help of explicit keyword.
Static is a non-access modifier. It means that something (a field, method, block or nested class) is related to the type
rather than any particular instance of the type.
The Static keyword can be applied to:
Static Method
Static Variable
Initialisation Block
Nested class
The super keyword in Java is a reference variable which is used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super
reference variable.
new keyword is used to create an instance of the class. In other words, it instantiates a class by allocating memory for a
new object and returning a reference to that memory.