ConstructorsQuiz.md - brainchildservices/curriculum GitHub Wiki

  1. Define Constructor.

    • A constructor is a special method of the class that gets automatically invoked whenever an instance of the class is created.
  2. Define the advantage of constructor.

    • The advantage of a constructor is that it is called when an object of a class is created. It can be used to set initial values for fields.
  3. Explain default constructor.

    • A constructor which has no argument is known as the default constructor. It is invoked at the time of creating object. Things inside the default constructor gets executed when the object is created.
  4. Explain Parameterized Constructor.

    • A constructor with at least one parameter is called a parameterized constructor.
  5. Explain the advantage of Parameterized Constructor.

    • The advantage of a parameterized constructor is that you can initialize each instance of the class with a different value.
  6. Define Constructor overloading.

    • It is the ability to redefine a Constructor in more than one form.
  7. Explain C# Destructors

    • The Destructor is also a special type of method present in a class, just like a constructor, having the same name as the class name but prefix with ~ tilde. The destructor in C# gets executed when the object of the class is destroyed.
  8. Explain garbage collection.

    • The garbage collector manages the allocation and release of memory for an application. For developers working with managed code, this means that you don't have to write code to perform memory management tasks.
  9. If you create 5 objects of a class, then how many time constructors will be called?

    • Constructor will be called 5 times on creating 5 objects of the class. On every object creation a constructor gets called.
  10. When you write a constructor, what return type do you write in constructor declaration?

    • No need to write a return type in a constructor declaration.
  11. What does GC collect () do?

    • It performs a blocking garbage collection of all generations. All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to try to reclaim the maximum amount of available memory.