Unit 3 Cycle 2 Day 1: I/II/III: Constructor Behavior
Share
I/II/III: Constructor Behavior
Section 3.2 — Constructors
Key Concept
I/II/III constructor behavior questions test multiple aspects of constructor execution: parameter handling, field initialization order, default values for uninitialized fields, and the implicit super() call. Each statement may assert something about what happens when a specific constructor is called. Evaluate each independently: trace the constructor execution including any this() or super() calls, determine the final state of all fields, and verify whether the statement's claim matches the traced result.
Consider the following class.
Which of the following statements are true?
I. new Item("Pen") creates an Item with qty = 1.
II. new Item("Pen", 5) creates an Item with qty = 5.
III. new Item() creates an Item with default values.
Answer: (A) I and II only
I: TRUE. The one-arg constructor sets qty=1.
II: TRUE. The two-arg constructor sets qty to the given value.
III: FALSE. There is no no-arg constructor. new Item() causes a compile error.
Why Not the Others?
(B) III is false. Once you define any constructor, Java does NOT provide a default no-arg constructor.
(C) I is also true. The one-arg constructor explicitly sets qty=1.
(D) II is also true. The two-arg constructor works as expected.
Common Mistake
Java provides a default no-arg constructor ONLY if you define NO constructors. Once you write any constructor, the default disappears. You must explicitly write a no-arg constructor if you need one.
AP Exam Tip
This is heavily tested: defining any constructor removes the default. If code calls new ClassName() but no no-arg constructor exists, it is a compile error.