Multiple choice technology programming languages

__________ will be automatically invoked when an object is created.

  1. method

  2. garbage collection

  3. finalizer

  4. Constructor

  5. none of the above

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Constructors are special methods invoked automatically when a new object is instantiated using the 'new' keyword. They initialize the object's state and set up required resources. Methods must be called explicitly, garbage collection runs automatically to reclaim memory, and finalizers are called before garbage collection (not during object creation).

AI explanation

To answer this question, you need to understand the concept of constructors.

Option A) method - This option is incorrect because a method is a block of code that performs a specific task. It is not automatically invoked when an object is created.

Option B) garbage collection - This option is incorrect because garbage collection is the process of automatically reclaiming the memory occupied by objects that are no longer in use. It is not invoked when an object is created.

Option C) finalizer - This option is incorrect because a finalizer is a special method that is automatically called before an object is garbage collected. It is not invoked when an object is created.

Option D) Constructor - This option is correct because a constructor is a special method that is automatically invoked when an object is created. It is used to initialize the state of the object and allocate memory for it.

Option E) none of the above - This option is incorrect because option D, Constructor, is the correct answer.

The correct answer is D) Constructor. This option is correct because a constructor is automatically invoked when an object is created.