Constructors

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 foll 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 defualt one with no parameters.

4.what is static constructor?
    
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 contructor is called first then Class A construcotr 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 B
              i am class A
8.What is Copy  construtor . 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 an 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 foll happens-
       New memory is allocated
       instance is created on heap
       reference is created on stack and passed on the 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 Framewrok classes like DateTime,FileStream et which throw exception from
      constructor.








22 comments:

  1. 7.In which order constructor is called in inheritance ?
    This Answer is wrong - Please verify.

    ReplyDelete
  2. yes IN Q7 first Constructor A is Called then B constructor

    ReplyDelete
  3. Q4 static constructor loaded during loading of class in CLR loader,before instantiation of class object.for example when you goes to college the college name is visible before becoming part of (instance of or before entry )college

    ReplyDelete
  4. Qus: 14 Default access modifier for constructor is Public. check your answer

    ReplyDelete
    Replies
    1. Default access modifier for constructor is Private as per MSDN.

      Delete
    2. By default constructor is private, have written a sample code and checked

      Delete
    3. This comment has been removed by the author.

      Delete
  5. All the answers provided here are not exact and correct answers. People preparing for interview don't follow this site.

    ReplyDelete
  6. In Question 7,First Class A is called then second

    ReplyDelete
  7. 7.In which order constructor is called in inheritance ?
    Answer is wrong .Answer will be i am class A,i am class B

    ReplyDelete
  8. Question 7 have wrong answer the correct output would be
    output- i am class A
    i am class B

    ReplyDelete
    Replies
    1. I agree with Gopal Singh. The output mentioned in the Question 7 will be possible when the constructors are declared as Static.

      Delete
  9. ans for Question 7 is wrong Parent class constructor called first : Refer Video 21 kudvenkat of c# tuts

    eg.

    class ClassA
    {
    static void Main()
    {
    ClassB objB = new ClassB();

    }
    public ClassA()
    {
    Console.WriteLine("i am class A");
    }
    }

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

    ReplyDelete
  10. Ans 12 is incorrect
    First the constructor that is called using this() operator will be called then the calling/parametrized constructor will be called

    ReplyDelete
  11. Q7 , Q12: answers are incorrect. Here are the correct answers verified by executing code:
    Q7. Ans: First Class A is called then second
    Q12. Ans: 1st constructor that is called using this() operator will be called then the calling/parametrized constructor will be called

    ReplyDelete
  12. Answer for Que. 7 is wrong. Correct answer is
    i am class A
    i am class B

    ReplyDelete
  13. Inheritance always follow parent to child.

    ReplyDelete
  14. Answer for que.12 is wrong.
    class numbers
    {
    public numbers() {
    }
    public numbers(int a):this(){
    }
    }
    Answer for this is it is called first default and then overloaded constructor will be called

    ReplyDelete