OOPS Interview Questions

Kailash Chandra Behera | Thursday, April 23, 2020


Here we will going to discuss the questions and answers of OOPS during interview, the interviewer is asking at the first round of interview. Most of the developer fails in this round, because every developer knows how to implement OOPS concept but at the time of discussion they are getting helpless to explain it properly.

Getting Started

Here we have tried to cover as much as questions related to OOPS, following are frequently asked Interview Questions for freshers as well as an experienced .net and Java Software Developers.

What is OOP?

The object oriented programming is commonly known as OOP. Most of the languages are developed using OOP concept. it is a programming concept that uses "objects" to develop a system.

An object in program has an ability to perform actions and has attributes. It performs just like real world entities for e.g. a motor bike. A bike performs actions such as ’Start’, ’Stop’ etc., and it has attributes like red color, 150 cc etc. So does an Object. Actions and attributes are represented by Methods and fields or properties respectively in programming language.

An object in real world hides the implementation details and exposes only the functionalities and parameters it requires to its client. Here also an object shares the same concept as that of a bike. While driving a motor bike, we are unaware of its implementation details such as how it is developed, internal working of gears etc.? We know only the functions or actions it can perform.

What are the various elements of OOP?
Below are the Various elements of OOP.

  1. Class
  2. Object
  3. Method
  4. Encapsulation
  5. Abstraction
  6. Polymorphism
  7. Inheritance
What is a class?

A class represents description of objects that share same attributes and actions. It defines the characteristics of the objects such as attributes and actions or behaviors. It is the blue print that describes objects.

What is an object?

An object is an entity that keeps together state and behaviors. For instance, a car encapsulates state such as red color, 900 cc etc and behaviors as ’Start’, ’Stop’ etc., so does an object.

An object is an instance of a class. If you consider .Dog.as a class, it will contain all possible dog traits, while object .German Shepherd. contains characteristics of specific type of dog.

What is Method?

Itis an object’s behavior. If you consider Dog as an object then its behaviors are bark, walk, run etc.

What is constructor?

A is a special type of method which is invoked when instance of class is created or you can say a constructor is a method used to initialize the state of an object. The name of constructor is same as class name and it has no return type.

Define Destructor?

A destructor is a method which is automatically called when the object is made of scope or destroyed. Destructor name is also same as class name but with the tilde symbol before the name.

Explain Encapsulation concept in OOP.

Encapsulation means keeping actions and attributes together under a single unit. This can also be understood using a motor bike example. A bike has actions such as ’switch on light’, ’horn’ etc. and attributes such specific color, size, weight etc. Here the actions and attributes are bundled together under a single unit, bike.

In a programming language, methods and properties that correspond to actions and attributes respectively are kept under a unit called object. The advantage of encapsulation is that the implementation is not accessible to the client. The user has to know only the functionality of encapsulated unit and information to be supplied to get the result.

Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers −
  1. Public :- Public access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class.
  2. Private :- Private access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members.
  3. Protected :- Protected access specifier allows a child class to access the member variables and member functions of its base class.
  4. Internal :- Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly.
  5. Protected internal :- The protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application.
Both Protected and Protected internal helps in implementing inheritance.

What is Abstraction in OOP?

Abstraction (also know as Information hiding) concept restricts direct exposure of data. Data is accessed indirectly using safe mechanism, methods in case of programming object.

Taking bike as an example, we have no access to the piston directly, we can use ’start button’ to run the piston. You can understand the advantage of information hiding concept from this example. If a bike manufacturer allows direct access to piston, it would be very difficult to control actions on the piston.

Note :- Abstraction and encapsulation are related features in object oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction.

What is an interface?

An interface is a collection of abstract method. If the class implements an inheritance, and then thereby inherits all the abstract methods of an interface.

Explain the term Polymorphism.

Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends on the data types used in the operation.

It can be static or dynamic. In static polymorphism, the response to a function is determined at the compile time. In dynamic polymorphism, it is decided at run-time.

C# provides two techniques to implement static polymorphism. They are
  1. Function overloading
  2. Operator overloading
An abstract class helps to implement Dynamic Polymorphism.

What is Function Overloading?

Function Overloading allows multiple functions to exist with same name but different parameters. Again if you take bike as an example, it has a function .Start. with two forms i.e. ’Auto Start’ and ’kick start’.

Explain Function Overriding.

Function Overriding means changing behavior of methods of base class in derive class by overriding the base class methods. If class A is a base class with method ’calculate’ and class B inherits class A, thus derives method ’calculate’ of class A. The behavior of ’calculate’ in class B can be changed by overriding it.

Define Inheritance.

Inheritance concept in OOP allows us to create a new class using an existing one. It also allows the new class to add its own functionality. This concept can also be related to real world entity. A bike manufacturer uses same mechanism of existing version of the bike while launching a new version with some added functionalities. This allows him to save time and efforts.

What are the advantages of OOP?
  1. It presents a simple, clear and easy to maintain structure.
  2. It enhances program modularity since each object exists independently.
  3. New features can be easily added without disturbing the existing one.
  4. Objects can be reused in other program.


No comments: