Interview questions for Oops c#

1. What is the difference between Interface and Abstract Class?

Some difference is listed below:
• An abstract class can have non-abstract methods (concrete methods) while in case of interface all the methods must be abstract.
• An abstract class can declare or use any variables while an interface is not allowed to do so.
• In an abstract class, all data member or functions are private by default while in interface all are public, we can’t change them manually.
• An abstract class use constructor while in an interface we don’t have any type of constructor.
• A class can implement any number of interfaces but a subclass can at most use only one abstract class.

2. What is OOP?

OOP stands for object-oriented programming language. It means that the program code is written in such a way that the methods, functions, and data is contained within an object.
This approach makes the program structure modular and easier to execute. It also helps in achieving the reusability concept i.e we don’t have to write a certain code again and again in the program as we can bind that code in a method in a class and use it for as many times as we want.

3. What are delegates and its uses?

A delegate Object is a reference type variable that use to holds the reference to a method. The reference can be changed at runtime which is held by an object of a delegate. A delegate object can hold many functions reference which is also known as Invocation List that refers functions in a sequence FIFO, we can new functions ref in this list at runtime by a += operator and can remove by -= operator.
Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System. Delegate class.
Let us move on to the next C# OOP Interview Questions
Popular Course in this category

4. Difference between abstract class and interface in C#?

An abstract class is an incomplete class whose object cannot be instantiated. The abstract class allows us to create functionality that subclasses can implement or override. We use a keyword abstract before we declare a class to be abstract.
• The interface is a blueprint of a class but it has no implementation. The things that are contained in the interface are the declarations of events, indexers, methods, or properties.
• An abstract class contains both declaration and definition part whereas an interface contains only the definition part.
• A class can only use one abstract class but many interfaces can be used in a class.
• An abstract class can not implement multiple inheritances whereas an interface can do so.

5. Explain early binding and late binding in C#?

Binding is the process in which association of method call to method body takes place.
The two types of binding are:
1. Early or static binding

o In early binding, the methods and properties are detected and checked during compile time itself. Also, the compiler knows about what kind of object it is.
o If a method or property does not match or exist or it has some data type problem, then an exception is thrown by the compiler during the compile time.
2. Late binding or dynamic binding
o In late binding, the methods and properties are detected and checked during runtime.
o Methods and properties which bypass compile-time checking are dynamic types that are detected during runtime.
o The compiler does not know what type of object or what types of methods and properties are contained in an object so it bypasses the compile-time checking which was handled by run time.
o This is also called polymorphism.

6. What are the different ways of method overloading?

Method overloading means when two or more than two methods have the same name but different parameters.
The three ways of method overloading are:
We can change the number of arguments or parameters.
For eg:
public static int sum(int num1, int num2)
{
//method body;
}
public static int sum(int num1, int num2, int num3)
{
// method body;
}
Copy
• We can change the data type of the arguments.
For eg:
public static int sum(int num1, int num2)
{
//method body;
}
public static double sum(double num1, double num2)
{
// method body;
}
Copy
• We can change the order of data types of arguments.
For eg:
public void address(string locality, int home_no)
{
// method body;
}
public void address( int home_no, string locality)
{
// method body;
}

View Course

7.  What is the difference between late binding and early binding?

In Compile time polymorphism or Early Binding, we will use multiple methods with the same name but a different type of parameter or maybe the number or parameter because of this we can perform different-different tasks with same method name in the same class which is also known as Method overloading.
public class TestData
{
public int Add(int a, int b, int c)
{
return a + b + c;
}
public int Add(int a, int b)
{
return a + b;
}
}
class Program
{
static void Main(string[] args)
{
TestData dataClass = new TestData();
int add2 = dataClass.Add(45, 34, 67);
int add1 = dataClass.Add(23, 34);
}
}
Dynamic / runtime polymorphism is also known as late binding. Here, the method name and the method signature (number of parameters and parameter type must be the same and may have a different implementation). Method overriding is an example of dynamic polymorphism.
public class Drawing
{
public virtual double Area()
{
return 0;
}
}
public class Square : Drawing
{
public double Length { get; set; }
public Square()
{
Length = 6;
}
public override double Area()
{
return Math.Pow(Length, 2);
}
}
public class Rectangle : Drawing
{
public double Height { get; set; }
public double Width { get; set; }
public Rectangle()
{
Height = 5.3;
Width = 3.4;
}
public override double Area()
{
return Height * Width;
}
}
class Program
{
static void Main(string[] args)
{
Drawing square = new Square();
Console.WriteLine(“Area :” + square.Area());
Drawing rectangle = new Rectangle();
Console.WriteLine(“Area :” + rectangle.Area());
}
}

8. What would happen in case if the inherited interfaces have conflicting method names?

This is the common C# OOP Interview Questions asked in an interview. if we have conflicting methods in the same class, we can’t implement their body independently in the same class because of the same name and same signature so we must use interface name before method name to remove this method confusion.
interface testInterface1 {
void Show();
}
interface testInterface2 {
void Show();
}
class Abc: testInterface1,
testInterface2 {
void testInterface1.Show() {
Console.WriteLine(“For testInterface1 !!”);
}
void testInterface2.Show() {
Console.WriteLine(“For testInterface2 !!”);
}
}

9. What are the different ways a method can be overloaded?

Method overloading is another name for compile-time polymorphism, where we can create a method with the same name but with a different signature. It is done at compile time and we can do it multiple ways but method name would remain the same.
A number of parameters can be different.
Type of parameter can be different.
Order of parameter can be different.
Example –
static int volume ( int x ){
return (x * x * x);
}
static double volume (float f, int i){
return 3.14 * f * f * I;
}
static long volume (long l, int b, int n){
return l * b * h;
}

leave your comment


Your email address will not be published. Required fields are marked *