Introduction C# OOP Interview Questions And Answers
C# is an Object-Oriented, functional, generic and component-oriented programming language. It is used to build a variety of applications, it is specifically strong at building Windows desktop applications and games. Web development can also be done efficiently with C# and it has increasingly become popular for mobile developments. So, it makes an excellent choice for any programmer who wishes to do web and game development. It is a statically-typed language, so written source code is thoroughly checked before it turns into an application. It is a complex language to learn and mastering it can take a substantial amount of time. There are various cross-platform tools available which allow applications written in C# which can be used over mobile and desktop.
If you are looking for a job that is related to C# OOP, you need to prepare for the 2019 C# OOP Interview Questions. Though every interview is different and the scope of a job is also different, we can help you out with the top C# OOP Interview Questions with answers, which will help you take the leap and get you success in your interview.
OOPS Interview questions with Answers for Freshers. OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is.
Below is the top 2019 C# OOP Interview Questions that are asked frequently in an interview these top questions are divided into two parts are as follows:
Web development, programming languages, Software testing & others
Part 1 – C# OOP Interview Questions (Basic)
This first part covers basic C# OOP Interview Questions and answers
1. What is the difference between Interface and Abstract Class?
Answer:
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 are delegates and its uses?
Answer:
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
4.5 (1,090 ratings)
3. What is a difference between late binding and early binding?
Answer:
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());
}
}
4. What would happen in case if the inherited interfaces have conflicting method names?
Answer:
This is the common C# OOP Interview Questions asked in an interview. if we have conflicting methods in a 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 !!');
}
}
5. What are the different ways a method can be overloaded?
Answer:
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;
}
Part 2 – C# OOP Interview Questions (Advanced)
This first part covers advanced C# OOP Interview Questions and answers
6. What is an accessibility modifier and how many are there in C#?
Answer:
Access modifiers are keywords used to specify the declared accessibility of a member or a type. In C# there are 5 several types of Access Modifiers.
Public – No restrictions on accessing public members.
Private – limited access within a class definition, if none is specified then this is the default.
Protected – access is limited to within class definition and any class that inherits from the class.
Internal – access is limited exclusively to the classes defined by the current project
Protected internal – member can be accessed anywhere in the same assembly and also accessible by inheriting that class, it can be accessible outside the assembly in derived class only.
7. What is a virtual method in C#?
Answer:
A virtual method is a method that can be redefined in derived classes. A virtual method has an implementation in a base class as well as derived from the class. It is used when a method’s basic functionality is the same but sometimes more functionality is needed in the derived class. A virtual method is created in the base class that can be overridden in the derived class. We create a virtual method in the base class using the virtual keyword and that method is overridden in the derived class using the override keyword.
When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member if no derived class has overridden the member. By default, methods are non-virtual. We can’t override a non-virtual method.
Let us move on to the next C# OOP Interview Questions
8. How to avoid NULL in C#?
Answer:
Null is not an object. We can have a class, but a variable with a NULL value means it’s not pointing to any object. We might come across a piece of code that contains many conditional statements that would check if the value of a variable is NULL. Let’s check a static method:
public static string DisplayUpperString( string s ){
string upper = string.Empty;
If( s null ) {
upper = null;
}
else {
upper = s.ToUpper();
}
return upper;
}
This code is fine, and convert to the upper case a given string.
But from an OO perspective, consider constructing an object that represents nothing, rather than evaluating it for NULL.
public static String DisplayUpperString ( PossibleString s ){
string upper = s.ToPossibleUpper();
return upper;
}
Now function is less cluttered, more readable, and no longer uses the check for a NULL value.
9. What is the extension method in C# and how to use them?
Answer:
This is the frequently asked C# OOP Interview Questions asked in an interview. An extension method is a special kind of static method, but they are called as if they were instance methods on the extended type. This method enables you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.
10. Can “this” keyword be used within a static method?
Answer:
Since “this” keyword returns a reference to the current instance of a class, so we cannot use this inside a static method. And static members exist without any instance of the class and call with the name of the class, not by instance. So, we can say that “this” keyword is a special type of reference variable that is implicitly defined within each constructor and non-static method as a first parameter of the type class in which it is defined.
Recommended Article
This has been a guide to List Of C# OOP Interview Questions and Answers so that the candidate can crackdown these C# OOP Interview Questions easily. Here in this post, we have studied about top C# OOP Interview Questions which are often asked in interviews. You may also look at the following articles to learn more –
Software Development Course - All in One Bundle
600+ Online Courses
3000+ Hours
Verifiable Certificates
Lifetime Access