In one of our previous article, we have provided C# interview questions and answers, now in this article, we will be focusing only on OOPS Interview questions which can be asked from C# Developers in an interview.

1. What is Object?

Objects are instances of Class, created using the keyword "new". You can create as many instances of a class as you would like.

When a new Object is created, a memory will be allocated for the class in heap memory area, they can be stored in either a named variable or in an array or collection, which is called as an instance and its starting address will be stored in the object in stack memory area.

Using the object of an class we call methods/properties of class to execute the program.

2. What is Object Oriented Programming in C#?

Object oriented programming means where Code is executed with the help of Objects and Classes.

C# uses concepts of Classes and object, where we create class for particular task for example "Student.cs" class is created where properties of students are declared "FirstName", "LastName" etc which is then accessed by creating class objects.

OOP also follows Encapsulatoin, Polymophism, Abstraction, Inheritance, which is implemented in C# using Classes, methods etc.

3. What is the difference between procedural and object-oriented programming?

Object-oriented Programming is a programming language that uses classes and objects to create models based on the real world environment. An Object-oriented Programming application may use a collection of objects which will pass messages when called upon to request a specific service or information. Objects are able to pass, receive messages or process information in the form of data.

Procedural Programming, which at times has been referred to as inline programming, takes a more top-down approach to programming. Procedural Programming takes on applications by solving problems from the top of the code down to the bottom. This happens when a program starts with a problem and then breaks that problem down into smaller sub-problems or sub-procedures. These sub-procedures are continually broken down in the process called functional decomposition until the sub-procedure is simple enough to be solved.

Source:Difference Between Procedural Programming and Object Oriented programming

4. What is Encapsulation?

Encapsulation means hiding complexities of program which is done in C# using Classes and Objects. 

For example, we can hide a method from accessing it outside of class by using "private" keyword, before defining method.

5. What is Polymorphism?

Poly means many, hence Polymorphism means one name different meaning.

We can achieve Polymorphism in C# by defining same method name, but with different argument types.

For example:

public int AddValues(int a, int b)
{
   //your code
}


public double AddValues(double  a, double  b)
{
   //your code
}


//as you can see the above methods have same name, but takes different arguments.

6. What is Abstraction?

Abstraction is just opposite of Encapsulation. Abstraction is a mechanism to show only relevant data to the user.

For example, when a user visits your website, you just show them buttons to navigate but not the functionality used when Button is clicked.

7. What is Inheritance?

Inheritance is a feature of object-oriented programming languages that allows you to define a base class that provides specific functionality (data and behavior) and to define derived classes that either inherit or override that functionality.

Example:

using System;

public class Base
{
   private int value = 10;

   public class Inherited : Base
   {
       public int GetValue()
       {
           return this.value;
       }
   }
}

public class SecondInheritation : Base
{
//    public int GetValue()
//    {
//        return this.value;
//    }
}

public class Example
{
    public static void Main(string[] args)
    {
        var b = new Base.Inherited ();
        Console.WriteLine(b.GetValue());
    }
}
// Output:
//       10

8. What is difference between Abstract Class and Interface in C#?

An Abstract class is a class whose objects can’t be created. It is a kind of guideline or a template for other classes. An abstract class can be considered as an incomplete class that does not represent complete behavior.

When Abstract class is derived by a class, we can define or not define methods of abstract class.

An Interface is another building block of C# which is a blueprint or template of a class.

When Interface is used by Derived class, it needs to define all methods, provided in Interface.

9. Can you declare an overridden method to be static if the original method is not static?

No, you cannot, because either both methods are static, or neither one and it is not valid for two methods with the same signature (name and parameter types) to have different modifiers such as static.

10. Does .NET support multiple inheritance?

No, .NET doesn't Support Multiple inhertiance because of Diamond problem.

Basically, if you have class A and B, both contains a method draw, and you inherited it in class C, which inherits draw method, now compiler doesn't know which draw method to execute, whether it should be executed from Class A or B.

11. What is Cohesion in C# OOP?

Generally, Cohesion means completeness with itself.

In C#, Cohesion is a measure of how closely related the members (classes, methods, functionality within a method) of a module are to the other members of the same module. It is desirable to increase cohesion as that indicates that a module has a very specific task and does only that task.

12. What is Coupling in C# OOP?

In General, Coupling means dependency on others.

So in C#, Coupling is a measure of how much a module (package, class, method) relies on other modules. It is desirable to reduce coupling, or reduce the amount that a given module relies on the other modules of a system.

cohesion-coupling-c-sharp-min.png

Image Source:StackOverflow.com