C# Interviw Example Code
C# Interviw Example Code
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);
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.
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 ;
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.
6) Define Property in C# ?
Properties are a type of class member , that are exposed to outside world as a pair of
Methods.For example for the static field Minsalary, we will Create a property.
private double minimumSalary;
public static double MinSalary
{
get
{
return minSalary;
}
set
{
minSalary = value;
}
}
get Method will get triggered and value in minimumSalary field will be returned.When we
execute ,
Employee. MinSalary = 3000;
set Method will get triggered and value will be stored in minimumSalary field .
7) Define Overloading in C# ?
When methods are created with same name , but with different signature its called overloading.
For example , WriteLine method in console class is an example for overloading. In first instance
, it takes one variable. In the second instance , WriteLine method takes two variable.
Console.WriteLine(x);
Console.WriteLine("The message is {0}", Message);
Let us call the operator overloaded functions from the method below. When first if condition is
triggered, first overloaded function in the rectangle class will be triggered. When second if
condition is triggered, second overloaded function in the rectangle class will be triggered.
public static void Main()
{
Rectangle obj1 =new Rectangle();
Rectangle obj2 =new Rectangle();
if(obj1 > obj2)
{
Console.WriteLine("Rectangle1 is greater than Rectangle2");
}
if(obj1 < obj2)
{
Console.WriteLine("Rectangle1 is less than Rectangle2");
}
}
What this means is that , all the methods and attributes of Base Class car is available in Derived
Class Ford. When an object of class Ford is created , constructors of the Base and Derived class
get invoked. Even though there is no method called DriveType() in Class Ford, we are able to
invoke the method because of inheriting Base Class methods to derived class.
13) Define Multiple Inheritance in C# ?
In C# , derived classes can inherit from only one base class. If you want inherit multiple base
classes, use interface.
14) What is Polymorphism in C# ?
Ability of a programming language to process objects in different ways depending on their data
type or class is known as Polymorphism. There are two types of polymorphism
1) Compile time polymorphism. Best example is Overloading
2) Runtime polymorphism. Best example is Overriding
15) Define Virtual Keyword in C# ?
When we want to give permission to derived class to override a method in base class, Virtual
keyword is used . For example lets us look at the classes Car and Ford as shown below.
class Car
{
public Car()
{
Console.WriteLine("Base Class Car");
}
public virtual void DriveType()
{
Console.WriteLine("Right Hand Drive");
}
}
class Ford : Car
{
public Ford()
{
Console.WriteLine("Derived Class Ford");
}
public void Price()
{
Console.WriteLine("Ford Price : 100K $");
}
public override void DriveType()
{
Console.WriteLine("Right Hand ");
}
}
When ever an instance of class car is created from the same class or its derived class(Except Few
Scenarios) Constructor get called and sequence of code written in the constructor get executed.
interface Breaks
{
void BreakType();
}
interface Wheels
{
void WheelType();
}
class Ford : Breaks, Wheels
{
public Ford()
{
Console.WriteLine("Derived Class Ford");
}
public void Price()
{
Console.WriteLine("Ford Price : 100K $");
}
public void BreakType()
{
Console.WriteLine("Power Break");
}
public void WheelType()
{
Console.WriteLine("Bridgestone");
}
}
Ref Parameter
If you want to pass a variable as ref parameter you need to initialize it before you pass it as
ref parameter to method. Ref keyword will pass parameter as a reference this means when
the value of parameter is changed in called method it get reflected in calling method also.
Declaration of Ref Parameter
Generally we will use ref parameters like as shown below
class Program
{
static void Main()
{
int i; // variable need to be initialized
i = 3;
Refsample(ref i);
Console.WriteLine(i);
}
public static void Refsample(ref int val1)
{
val1 += 10;
}
}
When we run above code we will get like as shown below
Output
*********Output************
13
*********Output************
As we discussed if ref parameter value changed in called method that parameter value
reflected in calling method also
Out Parameter
If you want to pass a variable as out parameter you dont need to initialize it before you
pass it as out parameter to method. Out keyword also will pass parameter as a reference
but here out parameter must be initialized in called method before it return value to calling
method.
Declaration of Out Parameter
Generally we will use out parameters like as shown below
class Program
{
static void Main()
{
int i,j; // No need to initialize variable
Outsample(out i, out j);
Console.WriteLine(i);
Console.WriteLine(j);
}
public static int Outsample(out int val1, out int val2)
{
val1 = 5;
val2 = 10;
return 0;
}
}
If we observe code we implemented as per our discussion like out parameter values must
be initialized in called method before it return values to calling method
When we run above code we will get like as shown below
Output
*********Output************
5
10
*********Output************