Constructor and Destructor – C# Programming Questions – Real

This section contains technical C# programming interview questions on constructor and destructor with answers and explanation asked in technical interviews for freshers and experienced.

Topic – Constructor and Destructor, Static Constructor in C#, Private constructors etc.


Q- What is output of the following program? When we create 2 objects of the class A e.g.

A obj1 = new A();
A obj2 = new A();

class A
{   
    static A()//Static constructor
    {
        Console.WriteLine("Static Constructor");
    }

    public A()// Constructor
    {      
        Console.WriteLine("Class Constructor");
    }    
}

Answer:

Output:

Static Constructor
Class Constructor
Class Constructor

Explanation: If we have static constructor in a class, it will be called before class constructor when we create first object of the class.

Static constructor will be called only once i.e. on first object creation and will not be called even we create multiple objects afterwards.

That’s the reason, in the output Static Constructor is called on first object creation.


Q- Explain output of the below program.

class Mobile
{
   public Mobile() { Console.WriteLine("Mobile: ctor"); }
    ~Mobile() { Console.WriteLine("Mobile: dtor"); }
}
class Moto : Mobile
{
    public Moto() { Console.WriteLine("Moto: ctor"); }
     ~Moto() { Console.WriteLine("Moto: dtor"); }
}
class MotoE : Moto
{    
    public MotoE () { Console.WriteLine("MotoE : ctor"); }
    ~MotoE () { Console.WriteLine("MotoE : dtor"); }
}

class Program
{
    static void Main(string[] args)
    {
        MotoE s = new MotoE();
       
    }   
}

Answer:

Output:

Mobile: ctor
Moto: ctor
MotoE: ctor
MotoE: dtor
Moto: dtor
Mobile: dtor

Explaination: In inheritance,order of class constructor call is from top to bottom. Means,from base class to most derived class. And for destructor, constructor call is reverse i.e. from most derived to base class.


Q- On execution of B b = new B();statement, what will be order of constructor call?

class A
{  
   public A() { Console.WriteLine("A: ctor"); }
   public A(int a) { Console.WriteLine("A:single parameter: ctor"); }
}

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

Answer: Output

A: ctor
B: ctor

Explanation: First the base class and then the derived class constructor will be called.

In fact, when the  statement B b = new B();  is executed,  the derived class constructor will be called automatically , but its body will not be executed, instead the control will go to the constructor of base class A and execute its body, then the control will return to the derived class constructor and executes its body.

So, first base class constructor gets executed, then derived class constructor.

NOTE:

In the base class A, we have two constructors i.e.  one empty and another one parameterized. But, from base class A empty constructor will be called as in derived class B, we have not call A’s parameterized constructor explicitly.

In derived class B, you define the constructor as “public B(){}” or “public B():base(){} in both cases, the empty constructor of base class will be called.

If you want to call the base class A parameterized constructor, then you have to modify the B class constructor as below:

public B():base(5){}

It should be noted that, base() or base(parameter) is used to call base class constructors.


Q- Tell me output with explaination of this program?

class A
{
   static A(){ Console.WriteLine("A:Static ctor "); }
   public A() { Console.WriteLine("A: ctor"); }
   public A(int a) { Console.WriteLine("A:single parameter: ctor"); }
}

class B : A
{
    public B() { Console.WriteLine("B: ctor"); }     
}
class Program
{
    static void Main(string[] args)
    {
        B s = new B();
       
    }   
}

Answer: output
A:Static ctor
A: ctor
B: ctor

Explaination:First derived class B’s constructor will be called and then A, But in class A, there is a static constructor also and static constructor always called before other constructors in a class. So, Static constructor call will be made first and then empty constructor.
No call for parameterized constructor in Class A will be made as it is not getting called from derived class B.


Q- What is wrong with this class methods? Explain your answer.

class Calc
{  
    public int result(){        
        return 5;
    }
    public float result()
    {
        return 5.0f;
    }
}

Answer:
In the class Calc, method overloading is programmed, but incorrectly. Compiler will fall in ambiguity during result() method call as overladed methods in this class, don’t differ by method singnature.
For method overloading, overloaded methods must differ either by number of arguments or the data type of at least one argument.


Related Posts