Friday, January 25, 2008

Another question....

class A
{
public A()
{
Console.WriteLine("Me A");
}
}
class B : A
{
public B()
{
Console.WriteLine("Me B");
}
}

If an object of class B is created which constructor will fire first?

Thursday, January 17, 2008

Enums - some more basics

Consider enum below
enum test:int
{
a,
b,
c = 5,
d
}

What will the following code display

static void Main(string[] args)
{
Console.WriteLine((int)test.a);
Console.WriteLine((int)test.b);
Console.WriteLine((int)test.c);
Console.WriteLine((int)test.d);
Console.ReadKey();
}

Wednesday, January 16, 2008

Question on hiding instance variable

Consider the class below

class joel
{
public int num = 0;
public void displaynum()
{
int num = 1;
}
public void displaynextnum()
{
num = 2;
}
}

What will below codes print and why?

1. joel j = new joel();
j.displaynum();
Console.WriteLine("Display member " + j.num);
Console.ReadKey();

2. joel j = new joel();
j.displaynextnum();
Console.WriteLine("Display member " + j.num);
Console.ReadKey();

Constructors and Destructors

1. Could we overload an instance constructor?

2. Does constructors have a return type

3. Could we inherit constructor?

4. When is a destructor invoked?

5. How are destructors invoked?

Tuesday, January 15, 2008

Question on exception / try - catch block

Question:

What will be the output for follwing code

try
{
Console.WriteLine("Before Exception");
throw new Exception("Raising exception");
Console.WriteLine("After Exception");
}
catch(Exception e)
{
Console.WriteLine("Inside Catch");
}
finally
{
Console.WriteLine("Inside Finally");
}
Console.WriteLine("Outside Try");

Answer:

Before Exception
Inside Catch
Inside Finally
Outside Try

Friday, January 11, 2008

.NET basics revisited......continued

Now question below is from a written test / interview

Question:
public void MergeSort(List list, int low, int high)
{
int temp;
if (low == high)
{
return;
}
if (high - low == 1)
{
if (list[low] > list[high])
{
temp = list[low];
list[low] = list[high];
list[high] = temp;
}
}
else
{
int mid = high + low / 2;
MergeSort(list, low, mid);
MergeSort(list, mid + 1, high);
}
}

a. run time exception
b. Will completely sort the list
c. Will partially sort the list


Answer: Stackoverflow exception, recursive call is going for an infinite loop.....
also please do comment if any other answers

Thursday, January 10, 2008

.NET basics revisited

I am on 'bench' these days.....well if thats not in your dictionary - 'its that condition of software engineer when he/she is not assigned to any projects / tasks'.

So thought of checking out tips, questions, tweaks, forums,interview questions etc in .NET....my next posts will be ones i have come across...so look out :)

Question: Which one among the following is illegal

a. Base b = new Derived();
b. Dervied b = new Base();

Where Derived is class inherited from Base...

Answer: b, now u can try and tell me

Question :
class A
{
public virtual void f()
{
Console.WriteLine("A.F");
}
}
class B : A
{
public override void f()
{
Console.WriteLine("B.F");
}
}
class C : B
{
new public virtual void f()
{
Console.WriteLine("C.F");
}
}
class D : C
{
public override void f()
{
Console.WriteLine("D.F");
}
}
class Program
{
static void Main(string[] args)
{
D d = new D();
A a = d;
B b = d;
C c = d;
a.f();
b.f();
c.f();
d.f();
}
}

What will this print

Answer: B.F B.F D.F D.F

If derived class overrides a method base class, objects of the derived class will call that method rather than the base class method.