Difficulty: Easy
Correct Answer: Creates a new instance of a class
Explanation:
Introduction / Context:A constructor is a special operation that initializes a new object. It sets up invariant state, injects dependencies, and prepares the object for immediate and correct use.
Given Data / Assumptions:
Concept / Approach:Instantiation consists of memory allocation followed by initialization. The constructor is invoked to perform initialization, enforce invariants, and establish default values. It does not update or delete existing objects; those responsibilities belong to setters, methods, or destructors/finalizers.
Step-by-Step Solution:
Identify the lifecycle phase: object creation.Bind the purpose of a constructor to initialization, not mutation or deletion.Select the option stating it creates a new instance.Verification / Alternative check:Language specifications and runtime behavior demonstrate that constructors are invoked only during object creation, never for existing instances.
Why Other Options Are Wrong:
Common Pitfalls:Doing heavy I/O in constructors; leaking references during construction; calling overridable methods from constructors and introducing partially constructed states.
Final Answer:Creates a new instance of a class
Discussion & Comments