Constructors Automatically Initialize Objects | Generated by AI

Home 2025.12

Question: Which method is called automatically when an object is created? A. main() B. Constructor C. finalize() D. toString()

Answer:

The correct answer is B. Constructor.

In object-oriented programming (particularly in languages like Java and C++), the constructor is a special method that is automatically invoked when an object of a class is created (instantiated using the new keyword in Java). Its primary purpose is to initialize the object’s state and allocate necessary resources.

This concept is fundamental in OOP for ensuring proper object initialization.

References:


Back

x-ai/grok-4.1-fast

Donate