Constructors - AP Computer Science A
- When is the default constructor provided?
- When no other constructors are defined. Java removes default constructor once any constructor is explicitly created.
- Find the issue: class C { C(int x) { this.x = x; } }
- No default constructor; must provide 'x'. Default constructor is removed when parameterized constructor exists.
- How are static fields initialized in constructors?
- Static fields are not initialized in constructors. Static fields belong to class, initialized once at class loading.
- Find the issue: class F { F() { super(); } }
- No issue if superclass has a default constructor. Explicit super() call is redundant but valid.
- What happens if 'super()' is not explicitly called?
- Java calls it automatically if not specified. Java inserts super() automatically if not explicitly written.
- What is constructor delegation?
- Using one constructor to call another in the same class. Reduces code duplication by calling other constructors.
- What is constructor overloading?
- Multiple constructors with different parameters. Provides flexibility for different initialization scenarios.
- How does the JVM handle constructor absence?
- Provides a no-argument constructor by default. Only when no constructors are explicitly defined by programmer.
- How does constructor chaining work?
- Using 'this()' to call another constructor in the same class. Must be the first statement in the constructor body.
- What is a constructor in Java?
- A special method to initialize objects. Called automatically when an object is created with 'new'.
- Which keyword is used to invoke a constructor?
- new. Creates a new object instance and calls the appropriate constructor.
- Which constructor is invoked first in inheritance?
- The parent class's constructor. Inheritance follows bottom-up constructor execution order.
- When is 'super()' required in a constructor?
- When the parent class has no default constructor. Must explicitly call parameterized parent constructor with super().
- Identify the visibility modifier typically used for constructors.
- public. Allows objects to be created from outside the class.
- How do you declare a constructor in Java?
- Same name as the class, no return type. Must match class name exactly and cannot have void or any return type.
- What is a parameterized constructor?
- A constructor with parameters to initialize fields. Accepts arguments to set specific initial values for fields.
- What is the role of 'super()' in a constructor?
- To call the parent class's constructor. Ensures parent class is properly initialized before child class.
- What is the effect of constructor overloading?
- Allows different initialization scenarios. Multiple ways to create objects with different initial states.
- Identify the issue: class J { J(int k) {} J(double k) {} }
- No issue; this is constructor overloading. Different parameter types allow valid constructor overloading.
- What is the primary function of a constructor block?
- To execute code for every new object instance. Runs initialization code every time an object is instantiated.
- What does 'this()' aim to resolve?
- Constructor chaining within the same class. Enables code reuse between constructors in same class.
- Which constructor type can be implicit?
- The default constructor. Java provides it automatically when no constructors exist.
- What is the main rule of constructor execution order?
- Base class constructors execute before derived class. Superclass initialization happens before subclass initialization.
- Identify the problem: class G extends H { G() {} }
- If H has no default constructor, error occurs. Depends on whether H has an accessible no-argument constructor.
- What happens if a constructor is private?
- Object creation is limited to the class itself. Prevents external instantiation, used in singleton pattern.
- Find the error: class I { I(String str) {} I() { this(str); } }
- 'str' is not accessible in 'I()'. Local parameter 'str' from other constructor is not accessible.
- How is constructor inheritance handled in Java?
- Constructors are not inherited in Java. Child classes must define their own constructors explicitly.
- What is a copy constructor?
- A constructor that creates a copy of an object. Takes another object as parameter to duplicate its state.
- What is the syntax to call a constructor within another constructor?
- Use 'this()' with appropriate parameters. Must be the first statement and match parameter types.
- Identify the error: class A { A() {} void A() {} }
- Method 'void A()' cannot be a constructor. Constructors cannot have return types, including void.
- How do you differentiate a constructor from a method?
- Constructors have no return type. Methods have return types; constructors initialize objects instead.
- Identify the issue: class D { D() { this(5); } D(int y); }
- Missing constructor body for 'D(int y)'. Constructor declaration lacks implementation body with braces.
- What is the default constructor?
- A no-argument constructor provided by Java. Automatically created by Java if no constructors are explicitly defined.
- Identify the use of the 'this' keyword in constructors.
- To refer to the current object's fields. Resolves naming conflicts between parameters and instance variables.
- Identify the purpose of a constructor.
- To initialize an object's fields. Sets initial values for instance variables when object is created.
- How can constructors enforce immutability?
- By initializing all fields and avoiding setters. Constructor sets final fields once, preventing later modification.
- Which class member is initialized using a constructor?
- Instance variables. Object fields that store the state of each instance.