Top 10 C# / .NET OOPS Interview Questions and Answers

Question 1. What is Object Oriented Programming (OOPS)?

OOPS is a technique to develop modules. These modules are assembled together as software.

OOPS works on the objects. Everything in OOPS is object. Object is made up of states and behaviors. Objects communicate with each other by sending messages to each other. Object hierarchy are built using Aggregation and Composition.

Some popular OOPS languages are:

  • C#
  • Java
  • Python

Question 2. What is Cohesion?

In OOPS we develop our code in modules. Each module has certain responsibilities. Cohesion shows how much a module responsibilities are strongly related. 

Higher cohesion is always preferred. Higher cohesion benefits are:

  1. Improves maintenance of modules
  2. Increase reusability

Question 3. What is Coupling?

OOPS Modules are dependent on each other. Coupling refers to level of dependency between two software modules.

Two modules are highly dependent on each other if you have changed in one module and for supporting that change every time you have to change in dependent module.

Loose Coupling is always preferred.

Inversion of Control and dependency injections are some techniques for getting loose coupling in modules.

Question 4. What is Abstraction?

Abstraction is a technique of taking something specific and making it less specific. 

In OOPS we achieve the abstraction by separating the implementation from interface. We take a implemented class and took only those method signatures and properties which are required by the class client. We put these method signatures and properties into interface or abstract class.

Question 5. What is Encapsulation?

In non object oriented languages, data and behaviors are not tied together. That means any function in the program can modify the data.

In Encapsulation, we bind the data and behaviors in one object. Only defined behaviors in a class can modify the data. We hide the state of an object by using properties and methods. Clients of the object only see these behaviors and by only these behaviors clients can modify the data.

We also protect the data by using access specifiers. We put the private / protected keywords before data to protect it from the outside world.

Question 6. What is Polymorphism?

Polymorphism is the ability of a Subtype to be treated as if it were an instance of the Supertype. Any method that accepts an instance of Supertype will also be able to accept an instance of Subtype without any casting required by the client side.

This allows multiple object of different subtypes to be treated as object of single super class, while automatically selecting the proper methods to apply to a particular object based on the subclass it belongs to.

There are mainly two advantages of polymorphism:

  1. We don't need to deal with family of types. We just need to specify the Supertype and we can assign any Subtype.
  2. We can later add multiple Subtypes that increase the extensibility of the program.

Question 7. What is Inheritance?

Inheritance is the ability to define a new class that inherits the behaviors of an existing class. The new class is a called Supertype and the original class is a Subtype.

Inheritance denotes a "is-a" relationship between classes.

Question 8. What is difference between Abstract class and Interface (Abstract vs Interface)?

Interface Abstract Class
All members are abstract. Some members are abstract some are fully implemented.
We can only define some members like events, properties, indexers and methods in an interface. We can define any member in Abstract class.
Interfaces supports multiple inheritance. Abstract class does not support multiple inheritance.
Interfaces are generic in nature. They can be implemented by any class for e.g. IClone interface can be implemented by any class like business objects, Database classes. Abstract classes are related. For e.g. ViewModelBase is an abstract class, then we knows this class will only inherits by ViewModels.

Question 9. What is difference between Composition and Aggregation (Composition vs Aggregation)?

Association is a relationship between two objects. This relationship may be one-o-one, one-to-many, many-to-one, and many-to-many.

For e.g. a student and teacher objects have an association.

Aggregation is a special case of association in which objects have a "has-a" relationship. 

Composition is a special case of Aggregation in which an object manages the life cycle of another object.

For example, a library contains students and books. Relationship between library and students is Aggregation. Relationship between library and books is composition. A student can exist without a library and therefore it is aggregation. A book cannot exists without a library and therefore it is composition.

Question 10. What is Is-A and Has-A relationship?

An inheritance relationship is an "is-a" relationship. A composition relationship is a "has-a" relationship.

Join our newsletter and get an occasional email with a technology and DotNetPattern.com news update.