C# OOPS Interview Questions and Answers

C# OOPS Interview Questions and Answers 111 Votes
   


Latest Basic and Advanced C# OOPS Interview Questions and Answers for Freshers and 2 - 4 years Experienced Dot Net developers and Testers with sample code on Access Modifiers, Static Members, Reference Types, Overloading, Constructor, Operator and Function Overloading, Encapsulation, Inheritance, Polymorphism, Overriding, Method Hiding, Abstract Class, Sealed Classes, Interfaces and Constructor.

1) What is a class ?

A class is the generic definition of what an object is . A Class describes all the attributes of object, as well as the methods that implements the behavior of member object. That means, class is a template of an object. Easy way to understand a class is to look at an example . In the class Employee given below, Name and Salary are the attributes of the class Person, Setter and Getter methods are used to store and fetch data from the variable.

public class Employee
{
private String name;
private String Salary;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name; }
public String getSalary ()
{
return Salary;
}
public void setSalary (String Salary)
{
this. Salary = Salary;
}
} 

2) What is an Object?

Object is an instance of a class, it contains real values instead of variables. For example, lets create an instance of class Employee called “John”.

Employee John= new Employee();
Now we can access all the methods in the class “Employee” via object “John” as shown below.
John.setName(“XYZ”); 

3) What are the Access Modifiers in C# ?

Different Access Modifier are - Public, Private, Protected, Internal, Protected Internal

  • Public – When a method or attribute is defined as Public , It can be accessed from any code in project. For example in the above Class “Employee” , getName(), setName() etc are public.
  • Private - When a method or attribute is defined as Private , It can be accessed by any code within the containing type only. For example in the above Class “Employee” , attributes name and salary can be accessed with in the Class Employee Only. If an attribute or class are defined without access modifiers , its default access modifier will be private.
  • Protected - When an attribute and methods are defined as protected, it can be accessed by any method in inherited classes and any method within the same class. The protected access modifier cannot be applied to class and interfaces. Methods and fields in a interface cannot be declared protected.
  • Internal – If an attribute or method is defined as Internal , Access is restricted to classes within the current project assembly
  • Protected Internal – If an attribute or method is defined as Protected Internal , Access is restricted to classes within the current project assembly and types derived from the containing class. 

Must Read.Net Framework Interview Questions and Answers 

4) Define Static Members in C# ?

If an attributes value had to be same across all the instances of the same class , static keyword is used. For example if the Minimum salary should be set for all employees in the employee class, use the following code

private static double MinSalary = 30000;

To access a private or public attributes or methods in a class, at first an object of the class should be created . Then using that instance of class , attributes or methods can be accessed. To access a static variable, we don't want to create an instance of the class having the static variable. We can directly refer that static variable as shown below.

double var = Employee.MinSalary ;


Must Read - ADO.Net Interview Questions and Answers 

5) Define Reference Types in C# ?

Let us explain this with an example . For the code given below,

Employee emp1;
Employee emp2 = new Employee();
emp1 = emp2;

Here emp2 has an object instance of Employee Class . But emp1 object is set as emp2. What this means is that object emp2 is refereed in emp1 and not that emp2 is copied into emp1. When a change is made in emp2 object, corresponding changes can be seen in emp1 object.