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 inheritance because they reasoned that adding multiple inheritance added too much complexity to C# while providing too little benefit.

Here is the 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

13. What is Operator Overloading in C#?

Operator overloading is a form of Polymorphism. Operator overloading means, the same operator can perform multiple functions depending on the operands type used with it.

For example, a + operator, when used with string type values, it will join string while if we will use + with int type operands, it will sum the values and give the result.

14. List the differences between method overriding and method overloading?

  Method Overloading Method Overriding
1. Creating more than one method or function having same name but different signatures or the parameters in the same class is called method overloading. Creating a method in the derived class with the same signature as a method in the base class is called a method overriding
2. It is called the compile-time polymorphism It is called runtime polymorphism
3. It has the same method name but with different signatures or the parameters It must have the same method name as well as the signatures or the parameters.
4. Method overloading doesn’t need an inheritance Method overriding needs inheritance
5. Method overloading is possible in single class only Method overriding needs hierarchy level of the classes i.e. one parent class and other child class.
6. Access modifier can be any Access modifier must be public.
7. Method overloading is also called early binding. Method overriding is also called late binding.

15. How can you prevent a class from overriding in C#?

By using the sealed keyword, when creating class.

16. Can you inherit Enum in C#?

No, you can't since Enum are "sealed" by default.

17. What is nested class?

Nested class is nothing but defining a class within another class.

Nested classes has the ability to specify private as an access modifier for the class itself.The use of the private access modifier defines the intended accessibility of the class and prevents access from outside the class.

18. What is the this Keyword in C#?

The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method.

Static member functions, because they exist at the class level and not as part of an object, do not have a this pointer. It is an error to refer to this in a static method.

Example:

class Vector3
{
    float x;
    float y;
    float z;

    public Vector3(float x, float y, float z)
    {
        this.x = x;
        this.y = y;
        this.z = z;
    }

}