Constructors Interview Questions

constructor in oops, constructor Interview Questions, why we need Constructor? what is copy Constructor? why we need copy Constructor? what is default Constructor? constructor in c#

Why we need a constructor in object-oriented programming?

The purpose of Constructor is to initialize the values of data members or to perform some logic when an instance of the class gets created. Yes, we can achieve this without constructor but for that, we need to make explicit calls and chances that someone might forget to call method to initialize the values before creating an instance of the class. On the other hand, constructors automatically get called whenever an instance gets created. 


What will happen if we do not explicitly provide a constructor for a class?

If the class does not have a constructor, C# will create one by default that instantiates the object and sets all the member variables to their default values. 


When should we provide the constructor explicitly?

The constructor is a special method that gets called automatically when we create an instance of a class. So, whenever there is a need to execute some logic at the time of object creation or to initialize some value of data members, we must provide the constructor explicitly.


What is the output of below program and why?

using System;
using System.Runtime.InteropServices;

namespace Demo
{
    class Animal
    {
        public Animal()
        {
            Console.WriteLine("Animal Class Constructor");
        }
    }


    class VegetarianAnimal: Animal
    {
        public VegetarianAnimal()
        {
            Console.WriteLine("Vegetarian Animal Class Constructor");
        }

    }

    class Rabit : VegetarianAnimal
    {
        public Rabit()
        {
            Console.WriteLine("Rabit Class Constructor");
        }

    }

    class ClientCar
    {
        static void Main()
        {
            
           Rabit rrRabit = new Rabit();
           Console.ReadLine();
        }
    }
}

Output of the programme will be 

Animal Class Constructor
Vegetarian Animal Class Constructor
Rabit Class Constructor

As we can see “Rabit” class is inheriting from the “VegetarianAnimal” class again “VegetarianAnimal” class is inheriting from the “Animal” class. So when we create an object of “Rabit” class it goes the parent a class of “Rabit” class i.e. “VegetarianAnimal” class and constructer of that class is get called similar way “VegetarianAnimal” class also has parent so it will call his parent to result into the above output


Can we throw exception from constructor?

Programmatically yes, we can throw an exception from constructor but constructors are not meant for it. I mean the purpose of the constructor is to initialize the objects so, it’s not a good idea to throw an exception while initializing value.


What is the output of the below program and why?

using System;

namespace Demo
{

    class Rabit 
    {
        private readonly string _name;
        private readonly string _color;

        public Rabit()
        {
            Console.WriteLine("Rabit Class Default Constructor");
        }

        public Rabit(string name):this()
        {
            Console.WriteLine("Rabit Class Parameterized Constructor 1");
        }

        public Rabit(string name,string color): this(name)
        {
            _name = name;
            _color = color;
            Console.WriteLine("Rabit Class Parameterized Constructor 2");
        }

    }

    class ClientCar
    {
        static void Main()
        {
            
           Rabit rrRabit = new Rabit("bunny");
           Console.ReadLine();
        }
    }
}

Output of above program will be 

Rabit Class Default Constructor
Rabit Class Parameterized Constructor 1

The reason for outputs is the above program is we have created instance with one parameter hence it will call respective overload constructor, but constructor will call the base constructor which populates above result.


Can child class inherit parent class constructor?

When a child class inherits from parent class it will inherit all the members including the constructor. 

When we have default or parameterless constructor in parent and child then we don’t need to explicitly inherit the class. It will do implicitly when creating a child instance.

In C# we can in inherit constructor using base keyword.

For example, in the below example we see “Rabit” class, inherits from the “VegetarianAnimal” class. And using base keyword we are passing the parameter which results in inheriting the constructor of the “VegetarianAnimal” class.

using System;
using System.Runtime.InteropServices;

namespace Demo
{
    class Animal
    {
        public Animal()
        {
            Console.WriteLine("Animal Class Constructor");
        }
    }


    class VegetarianAnimal: Animal
    {
        public VegetarianAnimal(string name)
        {
            Console.WriteLine("Vegetarian Animal Class Constructor");
        }

    }

    class Rabit : VegetarianAnimal
    {
        public Rabit(string name, int legs, string color) : base(name)
        {
            Console.WriteLine("Rabit Class Constructor");
        }

    }

    class ClientCar
    {
        static void Main()
        {
            
           Rabit rrRabit = new Rabit("bunny",4,"white");
           Console.ReadLine();
        }
    }
}

What is the output of below program?

using System;

namespace Demo
{

    class Rabit
    {
        private readonly string _name;
        private readonly string _color;

        public Rabit() : this("default", "default")
        {
            Console.WriteLine("Rabit Class Default Constructor");
        }

        public Rabit(string name) : this()
        {
            Console.WriteLine("Rabit Class Parameterized Constructor 1");
        }

        public Rabit(string name, string color) : this(name)
        {
            _name = name;
            _color = color;
            Console.WriteLine("Rabit Class Parameterized Constructor 2");
        }

    }

    class ClientCar
    {
        static void Main()
        {

            Rabit rrRabit = new Rabit("bunny");
            Console.ReadLine();
        }
    }
}

It will give a compile-time error because the above code result into cyclical constructor call and constructor cannot call itself from another constructor hence it will give a compile-time error


What is Constructor Overloading in C#?

C# supports constructor overloading that mean we can create multiple constructor using different signature. For example 

using System;

namespace Demo
{
    class Car
    {
        private string _name;
        private string _type;

        static Car()
        {
            Console.WriteLine("Static Constructor!");
        }

        public Car(string name, string type)
        {
            _name = name;
            _type = type;
        }

        public Car(Car c)
        {
            _name = c._name;
            _type = c._type;
        }
    }

    class ClientCar
    {
        static void Main()
        {
            Car car = new Car("Audi", "Sedan");
            Car car1 = new Car(car);
            Console.ReadLine();
        }
    }
}

What is the use of Copy Constructor?

You can read about Copy Constructor


Why we need copy constructor?

There may be situations where many values need to be passed for instantiating the class. Moreover, we need to instantiate such class objects more than once with the same values. So instead of passing the same values again to create new instances of such a class, we can create copy constructors to create such instances.


What is the use of Static Constructor?

You can read about Static Constructor


Read more about the constructor tutorial and take an online free constructor quiz to test your knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *