OOPs Interview Questions and Answers

Table of Contents

Introduction

This article has latest and most asked OOPs interview questions and answers.

OOPs is widely used and it is one of the most asked topics for technical interviews. Especially for important programming languages such as Java, C, Python etc.

I have prepared this list with short and crisp oops interview questions and answers so that it does not take much time to learn.

So let’s get started :

OOPs Interview Questions

  • What is Object Oriented Programming / OOP?
  • Why OOP paradigm ?
  • What are some of the main features of OOPs / What are basic concepts of OOP ?
  • What is Encapsulation ?
  • What is an Object ?
  • What is a class ?
  • What is Polymorphism ? How is it supported by C++ ?
  • What is Inheritance ? What is the purpose ?
  • What is Abstraction in OOPs?
  • What are Manipulators in OOPs ?
  • What is the constructor ?
  • What is a Destructor ?
  • What is an Inline function in OOPs ?
  • What is a virtual function?
  • What is a friend function in OOPs?
  • What is a function overloading ?
  • What is operator overloading concept?
  • What is an Abstract class in OOPs?
  • What is a ternary operator ?
  • Explain the use of finalize method ?
  • What are the different types of arguments ?
  • What is the super keyword ?
  • What is the concept of method overriding ?
  • What is an interface ?
  • What is exception handling in OOPs ?
  • What are tokens ?
  • Explain the main difference between overriding and overloading ?
  • Explain the main difference between a class and an object ?
  • What are the access modifiers ?
  • What are sealed modifiers ?
  • How to call base method without creating an instance ?
  • Explain the difference between new and override ?
  • What are the types of Constructors ?
  • What is the term early and late Binding ?
  • Explain the β€˜this’ pointer ?
  • What is the default access modifier of a class?
  • Explain the term pure virtual function ?
  • Explain the difference between a Class and a Structure ?
  • Which Operators cannot be overloaded ?
  • What is run time / dynamic polymorphism ?
  • What is a copy constructor ?
  • Is it necessary for a constructor to have parameters ?
  • Can static methods use the nonstatic members ?
  • What does the keyword virtual means in method ?
  • What does these terms defines – base class, subclass and superclass ?
  • Name the OOPs concept which exposes only the necessary information to the calling functions ?
  • Which OOPS concept provides Reusability ?
  • What is the default access modifier for a class?
  • Which keyword can be used for overloading?
  • How many instances can be created for an abstract class ?
  • What is static and dynamic Binding ?

What is Object Oriented Programming / OOP?

OOP stands for Object Oriented Programming. It is a programming paradigm.

In OOPs, the complete application / program is considered as a group of objects which are communicating with each other.

Object in this paradigm can be a real life entity. It is consists of data and methods. Methods operates on object’s data.

Why OOP paradigm ?

Nowadays, mostly all programming language are working in OOPs paradigm. This is because it has many advantages over traditional procedural programming.

Below are some of the key advantages :

  • The main OOP feature is that the code is better manageable.
  • The code written in OOPs paradigm is more understandable.
  • Object Oriented Programming has better maintenance as it has concepts like Encapsulation. It allows developers to change the representation by keeping the methods same.

OOPs paradigm works great with relatively big applications.

What are some of the main features of OOPs / What are basic concepts of OOP ?

  1. Encapsulation
  2. Polymorphism
  3. Abstraction
  4. Inheritance

What is Encapsulation ?

Encapsulation in OOPs paradigm is very useful concept. It is a feature that provides data binding.

This concept is actually an attribute of the object. It contains the hidden data for that object.

We can restrict the hidden data to class members.

It has levels such as Private, Public, Internal, Protected and Protected Internal.

This concept can be referred as :

  • Bundling data and method together : We bundle the data and its associated methods into one unit.
  • Data hiding : We restrict the access to members of an object bu using different levels like Public, Private etc.

What is an Object ?

An object is a real world entity. It is an instance of a class.

An Object has its own identity, state and behavior.

State typically represents data, behavior represents methods and the identity is a unique id associated with object.

What is a class ?

A class in OOPs paradigm is a template that represents the Objects.

It represents the type of object.

It is like blueprint which describes all the details of its object.

OOPS interview questions

What is Polymorphism ? How is it supported by C++ ?

Polymorphism in simple words means more than one form.

It is a concept of assigning behavior in a subclass to something which has already been declared in the main class.

Polymorphism concepts referred to as when some objects behave different with different contexts.

OOPS interview question

In C++ programming , below features supports the Polymorphism concept.

There are usually two types of Polymorphism :

  • Compile Time Polymorphism : Compile Time Polymorphism means that the compiler will know which function or method is to be called for which polymorphic call.
  • C++ programing supports compile time polymorphism as it has below mentioned features :
    • Function overloading.
    • Templates.
    • default arguments.
  • Run Time Polymorphism : This type of polymorphism is supported by virtual functions. Meaning, which function to be called at which polymorphic call is decided in Runtime.

What is Inheritance ? What is the purpose ?

Inheritance concept is very simple. It defines that a class can have its parent class and child class as well.

The idea is that a new class can be created which is based on another existing class. It uses data and implementation of its own and its parent class.

The basic and important purpose of inheritance is Code Reusability.

  • If the Inheritance concept applied to only one class then it is called as Single Inheritance.
  • It the Inheritance depends on multiple classes then it is called as Multiple Inheritance.

What is Abstraction in OOPs?

When we write programs then it is natural we will encounter some problems. Usually , the problems are like “real life” and we can provide some program to solve that problem to make life easier.

However, the problem are always not simpler and we require that we understand the complete problem, its details and the probable solution.

But we want this process to be separated from the necessary details which are already working.

Meaning, we are trying to obtain the abstract view of the problem.

This process is basically Abstraction in OOps.

Abstract refers to incomplete or partial view.

What are Manipulators in OOPs ?

Manipulator in OOPs paradigm is simply a function. This function is used on objects in conjunction with the operators such as extraction (>>) and insertion (<<).

Some example of Manipulator are :

  • setw.
  • endl.

What is the constructor ?

Constructor is similar to a method but have additional functionalities.

It is primarily used for initializing the state of an object. The Constructor automatically gets invoked as we create an Object.

There exists some rules for Constructor :

  1. Name of Constructor should be same as class name.
  2. Constructor explicitly has no return type.

What is a Destructor ?

The concept Destructor is also used in OOPs. It is opposite as of Constructor.

It is a method which gets called automatically as an object is destroyed.

The name of Destructor should also be same as class name but it has tilde symbol before its name.

What is an Inline function in OOPs ?

Inline function in OOPs is basically a technique. It is used by the compilers and the instructs.

This technique is used for inserting the complete body of a function wherever that function is used in the source code.

What is a virtual function?

Virtual function is a member function of a class.

The functionalities of a virtual function can be overridden in its derived class.

We can define any virtual function using a keyword “virtual“. We provide it at the time of declaration of function.

In C++ language, we can declare a virtual function using a token(virtual).

It can also be achieved in Python Language or C language by using functionality such as function pointers.

What is a friend function in OOPs?

In OOPs, A friend function is simply a friend of a class. It has access to Private, Public, private or protected data in the same class.

Suppose, If the function is defined outside that class cannot access such information.

The friend function is allowed to be declared anywhere in the class.

It does not get affected by any access control keywords such as public, private, or protected.

What is a function overloading ?

Function overloading is a concept where we have regular functions with same name which perform different tasks.

It is the process that allows the creation of multiple methods with the same name.

These methods differs by the input and output of the respective functions. But the name of functions is same.

Consider below Examples :

void sum(int&amp; x, int&amp; y);
 
void sum(double&amp; x, double&amp; y);
 
void sum(struct bob&amp; x, struct bob&amp; y);

In Java :

void sum(int x, int y);
 
void sum(double x, double y);

What is operator overloading concept?

Operator Overloading is a concept where different set of operators are applied to a function depending upon its arguments.

We can use Operator,-,* for passing through the function. These have their own precedence for execution.

What is an Abstract class in OOPs?

An Abstract class represents Abstraction concept. It is a incomplete class.

  • This class cannot be instantiated.
  • We cannot create the object of an abstract class.
  • Abstract class can be inherited.

An abstract class may or may not contain only one Abstract method.

Java programming language allows that only abstract method can exist in an abstract class but other languages allows both abstract and non abstract methods in the abstract class.

What is a ternary operator ?

A Ternary operator is an operator which takes three arguments.

The Arguments and results are dependent upon the function and these are of different data types.

The ternary operator is often called as a conditional operator as well.

Explain the use of finalize method ?

Finalize method is very useful in Programming languages.

It helps in performing cleanup operations on the resources which are currently not being used.

Finalize method is a protected method. This method can be accessed by the same class or its derived class.

What are the different types of arguments ?

An argument is also known as a parameter.

Argument is a variable passed into a method in the function declaration. These declared parameters are passed into the method body for using.

There are basically two types of parameters / arguments exists :

  • Call by Value : In this type, the Value passed as an argument will get modified only inside the function. It also returns the same value.
  • Call by Reference : In this type, the passed Value can be modified in both scopes inside and outside the function. It can return same or different value.

What is the super keyword ?

The Super keyword is used for invoking the overridden method , the parent class’s method.

By using super keyword, we can access the original method which is overridden in our current class.

We can also access the hidden members of parent / super class by using super keyword.

It basically forwards a call from current class constructor to the super class constructor.

What is the concept of method overriding ?

Method overriding is a concept of providing implementation of a super class method in the derived / subclass.

We can perform method overriding by using the same super class method name, same parameter and same return type in the sub class method.

What is an interface ?

An interface is simply a collection of abstract methods.

An interface can be implemented by a class. Meaning, a class can provide implementation for the methods of interface.

If a class implements an interface then it inherits all the methods of interface and thus it is liable to provide implementation to each method of the interface.

In Java , Interfaces are used for providing functionalities similar to multiple inheritances. As multiple inheritance is not directly supported in Java.

What is exception handling in OOPs ?

Exception is simply an unwanted event / like a error which can occur at any point of execution of the program.

There are various types of Exceptions, as mentioned below :

  • Runtime exception
  • Error exceptions
  • Compile time exceptions

These exceptions can be hadles by Exception Handling concept so that program can run uninterrupted.

Exception Handling has concept to handle exceptions such as try, catch, throw etc keywords.

What are tokens ?

A token is simply a element which cannot be broken into components. It is complete on its own.

A compiler has predefined tokens and it recognizes it by their name.

Below are some of the Example of tokens :

  • Keywords
  • identifiers
  • constants
  • string literals
  • operators

Even below characters are also considered as tokens :

  • Brackets
  • Commas
  • Braces
  • Parentheses

Explain the main difference between overriding and overloading ?

Overloading concept is static Binding. However, Overriding is a dynamic Binding concept.

Overloading is a concept where same name methods have different arguments in a class. It may or may not return the same value.

Overriding is a concept where same method name , same arguments and same return types method exists in the parent and child class both.

Explain the main difference between a class and an object ?

An object is an instance of a class itself. However, a class is the template which have collection of similar objects.

Objects is a real world entity but class is logical.

  • Class can not hold any information but Objects are able to hold multiple information.
  • A class can have properties and function but a object cannot.
  • Object does not have any sub objects concept, but a class can have sub classes.

What are the access modifiers ?

OOPs has concept of Access modifiers. The access modifier defines the scope of a variable or function.

The defined scope will indicate which objects or classes can access the variable or method.

Below are the access modifiers exists in OOPs paradigm :

  • Public
  • Private
  • Protected
  • Friend
  • Protected Friend
NameAccessibility from own classAccessibility from derived classAccessibility from others
PrivateYesNoNo
PublicYesYesYes
ProtectedYesYesNo

What are sealed modifiers ?

Sealed modifiers are similar to Access Modifiers. However , they cannot be inherited.

Sealed modifiers can be applied to :

  • properties
  • events
  • methods

Sealed modifier are not allowed to be used with static members.

How to call base method without creating an instance ?

Yes, we can call the base method without even creating an instance.

It is possible with the “Static methods“.

Explain the difference between new and override ?

Override is a concept of method overriding. This modifier helps in overriding the base class function.

The “new” modifier is used to instructs the compiler for using the implementation instead using base class implementation.

What are the types of Constructors ?

There exists three types of Constructors, which are :

  • Default Constructor : This type of constructor has no parameters.
  • Parameterized Constructor : This type of constructor has parameters. We can use this constructor for creating a new instance as well as passing values to the instance.
  • Copy Constructor : This is a special type of Constructor. It is used to create a new object as a copy of an existing object.

What is the term early and late Binding ?

Early Binding is the process where the assignment of values to the variables happen at design time.

Late Binding is the process where the assignment of values to the variables happen at run time.

Explain the β€˜this’ pointer ?

The pointer THIS is used to refer the current class object.

‘this’ keyword can be used to differentiate between the current and the global object. It refers to the current one.

What is the default access modifier of a class?

Any class would have by default ‘Private‘ access modifier to it.

Explain the term pure virtual function ?

Pure virtual function is a function which cannot be defined but it can be overridden in the derived class.

We can declare any virtual function as pure virtual function by using the operator =0.

See Below Example :

Virtual void functionOne() // Virtual

Virtual void functionTwo() = 0 // Pure virtual

Explain the difference between a Class and a Structure ?

Below are the difference between a class and a structure :

ClassStructure
The default access type of a class is private.The default access type of a Structure is public.
A class can be used for grouping data and methods. A structure is used for grouping data only.
Classes have data and methods both. Exclusively used for data only.
It requires strict validation. It does not require strict validation.

Which Operators cannot be overloaded ?

There exists some operators which cannot be overloaded, as mentioned below :

  • Member Selection (.)
  • Scope Resolution (::)
  • Member selection through a pointer to function (.*)

What is run time / dynamic polymorphism ?

Run time polymorphism or dynamic polymorphism is also known as Method overriding.

It is the process where the call to an overridden function is resolved at run time and not during compile time.

In method overriding, we have two or more methods with same name, same parameters , same signatures but different implementations in different classes.

What is a copy constructor ?

This is a special type of constructor. It is used for creating a new object as a replica of an existing object.

There always exists one copy constructor. It is either defined by system or user.

Is it necessary for a constructor to have parameters ?

No, It’s not.

There exists default constructor which have zero parameters.

Can static methods use the nonstatic members ?

No. It is not allowed.

What does the keyword virtual means in method ?

It defines that the method can be overridden.

What does these terms defines – base class, subclass and superclass ?

Base class is the root class. It is most generalized.

Subclass is the child class which is inherited from the base class.

Superclass is the parent class. Other class inherits its properties and behavior.

Name the OOPs concept which exposes only the necessary information to the calling functions ?

Encapsulation.

Which OOPS concept provides Reusability ?

Inheritance.

What is the default access modifier for a class?

Private.

Which keyword can be used for overloading?

Operator.

How many instances can be created for an abstract class ?

None. An abstract class cannot be instantiated.

What is static and dynamic Binding ?

Binding is a simple concept of binding name with a class.

Static Binding, also known as Early binding, is a binding where the name can be associated with the class during the compile time.

Dynamic Binding, also known as Late binding, is a binding where the name can be associated with the class during the execution time.

Further Readings :

Conclusion

We have learnt the complete OOps interview question guide with the short and crisp answers. This will help you in cracking your technical interviews in 2020 for languages like Java, C, Python etc.

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply