Constructors - AP Computer Science A

  1. When is the default constructor provided?
    When no other constructors are defined. Java removes default constructor once any constructor is explicitly created.
  2. 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.
  3. How are static fields initialized in constructors?
    Static fields are not initialized in constructors. Static fields belong to class, initialized once at class loading.
  4. Find the issue: class F { F() { super(); } }
    No issue if superclass has a default constructor. Explicit super() call is redundant but valid.
  5. What happens if 'super()' is not explicitly called?
    Java calls it automatically if not specified. Java inserts super() automatically if not explicitly written.
  6. What is constructor delegation?
    Using one constructor to call another in the same class. Reduces code duplication by calling other constructors.
  7. What is constructor overloading?
    Multiple constructors with different parameters. Provides flexibility for different initialization scenarios.
  8. How does the JVM handle constructor absence?
    Provides a no-argument constructor by default. Only when no constructors are explicitly defined by programmer.
  9. How does constructor chaining work?
    Using 'this()' to call another constructor in the same class. Must be the first statement in the constructor body.
  10. What is a constructor in Java?
    A special method to initialize objects. Called automatically when an object is created with 'new'.
  11. Which keyword is used to invoke a constructor?
    new. Creates a new object instance and calls the appropriate constructor.
  12. Which constructor is invoked first in inheritance?
    The parent class's constructor. Inheritance follows bottom-up constructor execution order.
  13. When is 'super()' required in a constructor?
    When the parent class has no default constructor. Must explicitly call parameterized parent constructor with super().
  14. Identify the visibility modifier typically used for constructors.
    public. Allows objects to be created from outside the class.
  15. 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.
  16. What is a parameterized constructor?
    A constructor with parameters to initialize fields. Accepts arguments to set specific initial values for fields.
  17. 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.
  18. What is the effect of constructor overloading?
    Allows different initialization scenarios. Multiple ways to create objects with different initial states.
  19. Identify the issue: class J { J(int k) {} J(double k) {} }
    No issue; this is constructor overloading. Different parameter types allow valid constructor overloading.
  20. 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.
  21. What does 'this()' aim to resolve?
    Constructor chaining within the same class. Enables code reuse between constructors in same class.
  22. Which constructor type can be implicit?
    The default constructor. Java provides it automatically when no constructors exist.
  23. What is the main rule of constructor execution order?
    Base class constructors execute before derived class. Superclass initialization happens before subclass initialization.
  24. 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.
  25. What happens if a constructor is private?
    Object creation is limited to the class itself. Prevents external instantiation, used in singleton pattern.
  26. 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.
  27. How is constructor inheritance handled in Java?
    Constructors are not inherited in Java. Child classes must define their own constructors explicitly.
  28. What is a copy constructor?
    A constructor that creates a copy of an object. Takes another object as parameter to duplicate its state.
  29. 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.
  30. Identify the error: class A { A() {} void A() {} }
    Method 'void A()' cannot be a constructor. Constructors cannot have return types, including void.
  31. How do you differentiate a constructor from a method?
    Constructors have no return type. Methods have return types; constructors initialize objects instead.
  32. 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.
  33. What is the default constructor?
    A no-argument constructor provided by Java. Automatically created by Java if no constructors are explicitly defined.
  34. 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.
  35. Identify the purpose of a constructor.
    To initialize an object's fields. Sets initial values for instance variables when object is created.
  36. How can constructors enforce immutability?
    By initializing all fields and avoiding setters. Constructor sets final fields once, preventing later modification.
  37. Which class member is initialized using a constructor?
    Instance variables. Object fields that store the state of each instance.

Study these flashcards