Constructor Interview Questions in C#

1.what is a Constructor?
   Constructor is a method that is called when an instance of an object is created.
   They have the same name as a class.
    eg-
        class Test
       {
             public Test ()
            {

            }
        }


2.Can a constructor have access modifier?
    Yes. The fall are access modifiers allowed -
     1. Public --This is called whenever a class is initialized.
     2. Private- This will prevent the class from being instantiated (creating objects)
     3. Protected-
     4. Internal--cannot be instantiated outside the assembly.
 
3.If a class has parametrized constructor do we need to have default constructor?
   C# provides a default constructor with no parameters if there are no explicit ones. But if we
   have a parameterized constructor we need to specify a default one with no parameters.
 4.what is static constructor?
static constructor loaded during loading of class in CLR loader, before instantiation of class object. For example, when you go to college the college name is visible before becoming part of (instance of or before entry) college
   

5.Can constructors be inherited?
    No.

6.How can you call a base class constructor from a child class?
   Child class can call base class constructor by using the keyword base.

  
7.In which order constructor is called in inheritance?
    If Class B inherits Class A, when you create object of Class B then Class B constructor is called first then Class A constructor is called.
  eg-
      class ClassA
{
    public ClassA()
    {
        Console.WriteLine("I am class A");
    }
}

class ClassB:ClassA
{
    public ClassB()
    {
        Console.WriteLine("I am class B");
    }
}

output- I am class A
              I am class B


8.What is Copy constructor. Do you have this in C#?
 C# does not provide copy constructor.


9.Can constructors be overloaded?
      Yes.


10.How do you overload a constructor?    
      Overload a constructor by specifying diff parameters.
eg- 
            class numbers
           {
                public numbers() {
                 }

               public numbers(int a){
              }
          }

11.How do you call one constructor from another in the same class?
      By using this keyword. eg-
      class numbers
      {
                public numbers() {
                 }

               public numbers(int a):this(){
              }
       }

12.When you create an object of the above class which constructor is called 
      first?
      When you instantiate the above class first overloaded constructor is called and then the
       default one is called.


13.Can a constructor call itself by using this keyword?
      N0 it will through a compile time error.

     
12.Does memory gets allocated when constructor is called?
       Yes using new operator.


13. what happens when you instantiate an object?
      When you create instance of a class using new operator fall happens-
       New memory is allocated
       instance is created on heap
       reference is created on stack and passed on the instantiated object.


14.What happens if "new" keyword fails while instantiating?
       it throws OutOfMemoryException.



14.What is default access modifier of constructor?
       Private


15.Is it Ok to throw an exception from a constructor?
      No its not preferred. Constructors are used mainly for initialization purpose. But there are
      certain .Net Framework classes like DateTime, FileStream et which throw exception from
      constructor.