Monday 17 February 2014

C#.NET - Object Oriented Programming interview questions with answers - Part 2

Hi All, I researched on .NET and collected important interview questions on C#.NET Object Oriented Programming. 
I am posting interview questions with answers parts by parts.


Please Find Other Interview Questions

C#.NET - Object Oriented Programming interview questions with answers - Part 1

C#.NET - Object Oriented Programming interview questions with answers - Part 3

What is static readonly fields?
-It is very suimilar to the constant fields.
- static readonly variables cannot assign value at complie time initilization.
- value will be assigned at runtime through the static constructor.
Note: Only the C# built-in types except System.Object may be declared as const.
Note: Use defined types classes, arrays and structs cannot be constant.
Note: Constants must be initialized at the time of declaration.
Note: You cannot intilize implicit typed local variable to the null
Ex: var name=null;
What is extension method? Explain about extension methods?
Extension methods enables you to add new methods to the existing type.
Extension methods are special kind of static methods but they are called as instance method.
Examples of extension methods are System.Collections.IEnumerable and System.Collections.Generic.IEnumerable types.
If you are using the extension methods in your code you should refer the namespace into your program.
Ex: To use extension methods of System.Collections.IEnumerable and System.Collections.Generic.IEnumerable types you should refer the using system.Linq namespace.
If your extension method has the same signature of the method inside the type your extension method will never called.
Can we implement an extension method in a non-static class?
No, you should implement an extension method inside the static class.
What are named arguments?
Instead of passing arguments in parameters position you can pass the arguments by using the parameters name.
Ex:
GetTotal(number1: 10, number2: 20)
GetTotal(number2: 20, number1: 10)
What are optional parameters?
You can omit the arguments for the optional parameters.
Compailor will take default values for the optional parameters.
Ex:
OptionalMethod(string required, int optional = 10)
You can call this method as follows
OptionalMethod("name", 30);
OptionalMethod("name");
What are constructors?
Constructors are called when you instantiate the class or struct.
Constructors have the same name as class or struct.
Used to set the default values of the data members of class or struct.
You can limit the class from instantiating.
Both class and struct allows multiple constructors with different arguments.
How could you prevent a class from instantiating?
We can prevent a class from instantiating by making constructor as private.
Note: structs cannot contain the default parameterless constructor because compiler automatically provide one.
How can you call the base constructor from the child constructor?
You can call it by using the base keyword.

public Manager():base()
{
}
In the derived class if you are not calling the base class constructor explicitly by using the base keyword compiler will call internally base class constructor using the base keyword like above program.
Note: To initialize a static class or a static fields you must create a static constructor.
Note: It is better to make data members as private because if you make them as public anyone can access it from outside. So, protect data members by making them as private and initialize them through the properties and make properties as public.
What is a default constructor?
A constructor which is not have any arguments.
Note: If your class doesn't have any default constructor. a default constructor will automatically generates and assign default values to the data members.
Note: All C# types including premitive types such as int and double, inherit from a sigle base Object type.
Note: Assemblies contains the executable code in the form of IL code, and symbolic information in the form of metadata.
What is the default access level of constructor?
private
Note: Generally private constructors used in the classes that contains the static members only. Like Math class. Match class have static members and methods.
Note: We can use a private constructor to initialize the static members of the class.
Can you inherit the class which is have the private constructor?
No, you cannot.
In which type of situations we use private constructors?
If you want to prevent the user from instantiating the class we can use private constructor.
In which situtation you will use static constructor?
Generally in the situation when you want to initialize the static data of the class.
Explain about destructor?
Destructor is used to released the instances of the class.
Structs doesnt support the destructors.
A class should have only one destructor.
Destructor will call automatically by the garbage collector.
Destructor cannot be overloaded and inherited.
Destructor doesn't have modifiers as well as parameters.
Destructor internally call the Finalize() method.
What Object initializers do?
Object initializers allows us to access the fields and properties of the class at the time of the object creation. Instead of declaring the parametarised constructor in the class to assign the values to the fields and properties we can use the Object initializers.
Note: While you initializing data to the members of the class using object initilizers compiler will call the default constructor of the class.
So, If a class have the private default constructor object initializers will give the compile time error.
What is a Nested Type?
A type wich is declared under the class or the struct.
By default nested types are private.
You can get all the parent type members inside the nested type.
You can create the object of the nested type as follows.
Parent.Nested obj=new Parent.Nested();
Note: Partial method declaration must starts with the contextual keyword and must return the void.
Partial methods cannot be virtual.
Partial methods can be static.
Explain Anonymous Types?
Anonymous types are introduced in C# 3.0.
These are the datatypes generated at runtime by the compiler.
These are helpful in LINQ queries.
EX:
var employees=from employee in Employees
select new {employee.Name, employee.Salary}
Instead of selecting the whole properties of the employee we can select few of them.
Note: Value types donnot allow inheritance
Note: ?? operator used to compare with nullable types.
Note: Static methods cannot be inherited or oveerriden.
Explain about partial method?
Partials methods are defined their signature in once partial type and implemented in another partial type.
If partial method has no implementation the compiler will remove the partial method from the class at compile time.

Paritial methods are always private.
Partial methods must return void.
In both signature and implemtation signature should match.
Assume two interfaces have one method with same name and return type.

interface IInterface1
{
void Method1();
}
interface IInterface2
{
void Method1();
}
you are inheriting those two interfaces into one class.

class Program:IInterface1,IInterface2
{
}
How can you implement those methods in your derived class and how can you call the respected methods of interfaces?
interface IInterface1
{
void Method1();
}
interface IInterface2
{
void Method1();
}
class Program:IInterface1,IInterface2
{
static void Main()
{
IInterface1 aa = new Program();
aa.Method1();
IInterface2 aa2 = new Program();
aa2.Method1();
}

void IInterface1.Method1()
{
Console.WriteLine("Methods of IInterface1");
}
void IInterface2.Method1()
{
Console.WriteLine("Methods of IInterface2");
}
}

// Output
// Methods of IInterface1
// Methods of IInterface2
What is encapsulation?
Wrapping up of a data into a single unit (i.e. class) is called encapsulation.

It is a technique to hide the internal members of a class.

We can hide the private information and we can show the public information using encapsulation.
EX: hiding the properties and exposing the property to access the private data.
Note: If your class have only parameterised constructors and you are you are trying to instantitiate the class with out the parameters the compiler won't allow to create it.
for that you shold explicitly define the default constructor.
How can you call the local constructor from another constructor?
using this keyword.
Ex:
public Employee()
{
//...............
}
public Employee(string name):this()
{

}
What is instance constructor?
A constructor which is used to initialize the instance members of the class.
Note: Data members should be private and should be access only through the class methods and functions.
Note: To initialize a static class or a static fields you must create a static constructor.
Note: It is better to make data members as private because if you make them as public anyone can access it from outside. So, protect data members by making them as private and initialize them through the properties and make properties as public.
What is a default constructor?
A constructor which is not have any arguments.
Note: If your class doesn't have any default constructor. a default constructor will automatically generates and assign default values to the data members.
Note: All C# types including premitive types such as int and double, inherit from a sigle base Object type.
Note: Assemblies contains the executable code in the form of IL code, and symbolic information in the form of metadata.
What is the default access level of constructor?
private
Note: Generally private constructors used in the classes that contains the static members only. Like Math class. Match class have static members and methods.
Note: We can use a private constructor to initialize the static members of the class.
Can you inherit the class which is have the private constructor?
No, you cannot.
In which type of situations we use private constructors?
If you want to prevent the user from instantiating the class we can use private constructor.
In which situtation you will use static constructor?
Generally in the situation when you want to initialize the static data of the class.
Explain about destructor?
Destructor is used to released the instances of the class.
Structs doesnt support the destructors.
A class should have only one destructor.
Destructor will call automatically by the garbage collector.
Destructor cannot be overloaded and inherited.
Destructor doesn't have modifiers as well as parameters.
Destructor internally call the Finalize() method.
What Object initializers do?
Object initializers allows us to access the fields and properties of the class at the time of the object creation. Instead of declaring the parametarised constructor in the class to assign the values to the fields and properties we can use the Object initializers.
Note: While you initializing data to the members of the class using object initilizers compiler will call the default constructor of the class.
So, If a class have the private default constructor object initializers will give the compile time error.

No comments: