Skip to content
Sharp

Object-oriented programming · interview question

When should you use a constructor in a Java class?

Multiple choiceMediumchapter-2-curatedpremiumoopobject-references
Choose one answer
Answer and explanation

Correct answer

A. To initialize an object's state when it is created.

Why

Constructors are used in Java to initialize the state of an object when it is created. They set up initial values for the object's fields and are invoked automatically when a new object is instantiated.

Test yourself on Object-oriented programming

A free test with instant, explained results. No account needed.

Start free test
All Object-oriented programming questions →