Thursday, April 2, 2009

C# Interview Questions Part II

1. How to implement getCommon method in class a? Are you seeing any problem in the implementation?

Ans:
public class a:ICommonImplements1,ICommonImplements2
{
public int getCommon()
{
return 1;
}
}

interface IWeather
{
void display();
}
public class A:IWeather
{
public void display()
{
MessageBox.Show("A");
}
}
public class B:A
{
}
public class C:B,IWeather
{
public void display()
{
MessageBox.Show("C");
}
}
2. When I instantiate C.display(), will it work?

Ans. interface IPrint
{
string Display();


}
interface IWrite
{
string Display();
}
class PrintDoc:IPrint,IWrite
{
//Here is implementation
}

3. How to implement the Display in the class printDoc (How to resolve the naming Conflict) A: no naming conflicts

class PrintDoc:IPrint,IWrite
{
public string Display()
{
return "s";
}
}
interface IList
{
int Count { get; set; }
}
interface ICounter
{
void Count(int i);
}
interface IListCounter: IList, ICounter {}
class C
{
void Test(IListCounter x)
{
x.Count(1); // Error
x.Count = 1; // Error
((IList)x).Count = 1; // Ok, invokes IList.Count.set
((ICounter)x).Count(1); // Ok, invokes ICounter.Count
}
}
4. Write one code example for compile time binding and one for run time binding? What is early/late binding?

Ans. An object is early bound when it is assigned to a variable declared to be of a specific object type. Early bound objects allow the compiler to allocate memory and perform other optimizations before an application executes.
' Create a variable to hold a new object.
Dim FS As FileStream
' Assign a new object to the variable.
FS = New FileStream("C:\tmp.txt", FileMode.Open)
By contrast, an object is late bound when it is assigned to a variable declared to be of type Object. Objects of this type can hold references to any object, but lack many of the advantages of early-bound objects.
Dim xlApp As Object
xlApp = CreateObject("Excel.Application")

5. Can you explain what inheritance is and an example of when you might use it?

6. How can you write a class to restrict that only one object of this class can be created (Singleton class)?

Ans. Access specifiers

7. What are the access-specifiers available in c#?

Ans. Private, Protected, Public, Internal, Protected Internal.

8. Explain about Protected and protected internal, “internal” access-specifier?

Ans. protected - Access is limited to the containing class or types derived from the containing class.
internal - Access is limited to the current assembly.
protected internal - Access is limited to the current assembly or types derived from the containing class.

No comments: