Constructors

Help Questions

AP Computer Science A › Constructors

Questions 1 - 10
1

In the BankAccount code below, how does constructor chaining work in this example?


public class BankAccount {

    private String accountNumber;

    private double balance;

    // Public default constructor: creates an account with a default balance of 0.0

    public BankAccount() {

        this("000000", 0.0); // Chain to the parameterized constructor

    }

    // Public parameterized constructor: initializes both fields from arguments

    public BankAccount(String accountNumber, double balance) {

        this.accountNumber = accountNumber;

        this.balance = balance;

    }

    // Private constructor: internal helper for creating a "closed" account

    private BankAccount(String accountNumber) {

        this(accountNumber, 0.0); // Chain to the parameterized constructor

    }

}

The default constructor calls itself until fields are set.

The default constructor uses this(...) to call the two-parameter constructor.

The parameterized constructor uses this(...) to call the default constructor.

The private constructor can be called directly from any class.

Explanation

This question tests understanding of Java constructors in AP Computer Science A, focusing on constructor chaining and the this() keyword. Constructor chaining in Java allows one constructor to call another constructor in the same class using this(...), which must be the first statement in the constructor. In the provided BankAccount code, the default constructor uses this("000000", 0.0) to call the two-parameter constructor, passing default values for account number and balance. Choice B is correct because it accurately describes how the default constructor chains to the parameterized constructor using this(...). Choice A is incorrect because constructors cannot call themselves recursively - this would cause infinite recursion. To help students: Emphasize that this(...) must be the first line in a constructor and demonstrate tracing through constructor chains step by step. Watch for: confusion between this() for constructor chaining and this keyword for instance references.

2

In the Car code below, which constructor will be invoked when creating a new instance with no arguments?


public class Car {

    private String make;

    private String model;

    private int year;

    // Public default constructor: sets year to the current year

    public Car() {

        this("Unknown", "Unknown", 2026); // Chain to parameterized constructor

    }

    // Public parameterized constructor: initializes all fields

    public Car(String make, String model, int year) {

        this.make = make;

        this.model = model;

        this.year = year;

    }

    // Private constructor: internal helper for creating a placeholder car

    private Car(String make, String model) {

        this(make, model, 2026); // Chain to parameterized constructor

    }

}

The private two-parameter constructor is invoked.

No constructor runs because fields have default values.

The public no-argument constructor is invoked.

The public three-parameter constructor is invoked.

Explanation

This question tests understanding of Java constructors in AP Computer Science A, focusing on constructor invocation and method signatures. When creating an object with new Car(), Java looks for a constructor that matches the provided arguments - in this case, no arguments means it searches for a no-argument constructor. In the provided Car code, there is a public no-argument constructor that chains to the three-parameter constructor with default values. Choice C is correct because new Car() invokes the public no-argument constructor, which then chains to the parameterized constructor. Choice B is incorrect because while the three-parameter constructor eventually runs due to chaining, it's not directly invoked - the no-argument constructor is invoked first. To help students: Use diagrams showing the flow of constructor calls and emphasize matching constructor signatures to arguments. Watch for: students confusing which constructor is initially invoked versus which constructors run due to chaining.

3

Refer to the Car class code below: what is the purpose of the private Car(String, String, int) constructor?


public class Car {

    private String make;

    private String model;

    private int year;

    // Public default constructor: uses placeholder values.

    public Car() {

        this("Unknown", "Unknown");

    }

    // Public parameterized constructor: supplies a default year.

    public Car(String make, String model) {

        this(make, model, 2026);

    }

    // Private full constructor: centralizes initialization in one place.

    private Car(String make, String model, int year) {

        this.make = make;

        this.model = model;

        this.year = year;

    }

}

To replace the need for any public constructors

To make year default to 0 for every new Car object

To allow outside code to set year without parameters

To centralize field initialization used by other constructors

Explanation

This question tests understanding of Java constructors in AP Computer Science A, focusing on the purpose of private constructors in design patterns (AP CSA standard). Private constructors serve specific design purposes - they cannot be called directly from outside the class but can be used internally for constructor chaining. This pattern centralizes initialization logic in one place, reducing code duplication and ensuring consistency. In the Car class, the private Car(String, String, int) constructor contains all field initialization logic, and both public constructors chain to it. This ensures all Car objects are initialized through the same code path. Choice A is correct because the private constructor centralizes all field initialization, allowing other constructors to reuse this logic through chaining. Choice B is incorrect because private constructors cannot be called from outside the class. To help students: Explain the benefits of centralized initialization. Show how private constructors support the DRY principle. Practice identifying when to use private constructors. Watch for: misunderstanding private access, not recognizing the code reuse pattern.

4

Refer to the Car class code below: which constructor will be invoked when creating a new Car with no arguments?


public class Car {

    private String make;

    private String model;

    private int year;

    // Public default constructor: sets a reasonable default year.

    public Car() {

        this("Unknown", "Unknown"); // Constructor chaining to reuse initialization

    }

    // Public parameterized constructor: sets make and model, and uses current year.

    public Car(String make, String model) {

        this(make, model, 2026); // Constructor chaining to set all fields

    }

    // Private full constructor: centralizes all field initialization.

    private Car(String make, String model, int year) {

        this.make = make;

        this.model = model;

        this.year = year;

    }

}

The private Car(String, String, int) constructor

No constructor runs because fields have defaults

The public Car() constructor

The public Car(String, String) constructor

Explanation

This question tests understanding of Java constructors in AP Computer Science A, focusing on constructor invocation and chaining (AP CSA standard). Constructors in Java are special methods used to initialize objects when they are created. They have the same name as the class and no return type. Constructor chaining allows one constructor to call another using this() to reuse initialization logic. In the provided Car class, when new Car() is called with no arguments, the public Car() constructor is invoked. This constructor immediately chains to Car(String, String) using this("Unknown", "Unknown"), which then chains to the private Car(String, String, int) constructor. Choice C is correct because the public Car() constructor is the entry point for no-argument object creation. Choice A is incorrect because the private constructor cannot be directly invoked from outside the class - it's only accessible through constructor chaining. To help students: Draw diagrams showing the flow of constructor chaining. Practice tracing through constructor calls step by step. Emphasize that the first constructor called matches the arguments provided. Watch for: confusion about which constructor runs first versus which does the actual initialization.

5

Refer to the BankAccount class code below: what value will the field balance have after executing new BankAccount()?


public class BankAccount {

    private String accountNumber;

    private double balance;

    // Public default constructor: sets a default account and zero balance.

    public BankAccount() {

        this("000000", 0.0);

    }

    // Public parameterized constructor: initializes both fields.

    public BankAccount(String accountNumber, double balance) {

        this.accountNumber = accountNumber;

        this.balance = balance;

    }

    // Private constructor: blocks constructing with only a balance.

    private BankAccount(double balanceOnly) {

        this("000000", balanceOnly);

    }

}

It will be 0.0 after the default constructor runs

It will be 100.0 because balance defaults to a deposit

It will be uninitialized because balance is private

It will be null until a setter method is called

Explanation

This question tests understanding of Java constructors in AP Computer Science A, focusing on field initialization through constructor chaining (AP CSA standard). When constructors chain, the final constructor in the chain performs the actual field assignments. Understanding how values flow through constructor chains helps predict object state after construction. Primitive fields like double have default values but constructors typically set explicit values. In the BankAccount class, new BankAccount() invokes the default constructor, which chains to BankAccount(String, double) with arguments ("000000", 0.0). The two-parameter constructor sets balance to 0.0. Choice A is correct because the constructor chain explicitly sets balance to 0.0. Choice C is incorrect because primitive types like double cannot be null - they always have a value, and here it's explicitly set to 0.0. To help students: Trace field values through constructor execution. Emphasize the difference between primitive and reference type defaults. Practice predicting final field values after construction. Watch for: confusing primitive types with reference types regarding null, forgetting that constructors override default values.

6

Refer to the Student class code below: how does the parameterized constructor differ from the one-parameter constructor?


public class Student {

    private String name;

    private int id;

    // Public constructor: sets name only and defaults id.

    public Student(String name) {

        this(name, 0);

    }

    // Public parameterized constructor: sets both name and id.

    public Student(String name, int id) {

        this.name = name;

        this.id = id;

    }

    // Private constructor: blocks creating a student with no data.

    private Student() {

        this("Unknown", 0);

    }

}

It can be called only inside the Student class

It is the default constructor because it has two parameters

It initializes both name and id from arguments

It sets id to 0 and ignores the passed id

Explanation

This question tests understanding of Java constructors in AP Computer Science A, focusing on comparing constructor functionality and parameter handling (AP CSA standard). Different constructors in the same class provide flexibility in object initialization - some may set all fields, while others provide defaults for certain fields. Understanding these differences helps in choosing the appropriate constructor for specific needs. In the Student class, the one-parameter constructor Student(String) only accepts a name and defaults id to 0, while the two-parameter constructor Student(String, int) accepts and sets both name and id from arguments. Choice A is correct because the parameterized constructor initializes both fields using the provided arguments, giving full control over initial values. Choice C is incorrect because the two-parameter constructor uses the passed id value, not 0. To help students: Create tables comparing what each constructor sets. Show examples of when to use each constructor. Practice choosing constructors based on initialization needs. Watch for: assuming all constructors behave identically, misreading which parameters are used versus defaulted.

7

Refer to the Student class code below: what value will the field id have after executing new Student("Ana")?


public class Student {

    private String name;

    private int id;

    // Public constructor: sets name only, defaults id to 0.

    public Student(String name) {

        this(name, 0); // Constructor chaining to reuse full initialization

    }

    // Public parameterized constructor: sets both name and id.

    public Student(String name, int id) {

        this.name = name;

        this.id = id;

    }

    // Private default constructor: prevents creating a nameless student.

    private Student() {

        this("Unknown", 0);

    }

}

It will be the length of the name string

It will be -1 because id is unassigned

It will be 0 after constructor chaining completes

It will be an unpredictable default value

Explanation

This question tests understanding of Java constructors in AP Computer Science A, focusing on constructor chaining and field initialization (AP CSA standard). Constructors initialize object fields when instances are created, and constructor chaining using this() allows code reuse between constructors. When one constructor calls another, the final constructor in the chain performs the actual field assignments. In the Student class, new Student("Ana") calls the one-parameter constructor, which chains to Student(String, int) with this("Ana", 0). The two-parameter constructor then sets name to "Ana" and id to 0. Choice A is correct because the constructor chain explicitly passes 0 as the id value. Choice B is incorrect because Java initializes int fields to 0 by default, but here the constructor explicitly sets it to 0 anyway. To help students: Trace through constructor chains step by step. Show how parameters flow through the chain. Practice predicting field values after construction. Watch for: forgetting that constructor chaining passes specific values, assuming uninitialized fields remain at Java defaults when constructors explicitly set them.

8

Refer to the Car class code below: what value will the field year have after executing new Car("Honda", "Civic")?


public class Car {

    private String make;

    private String model;

    private int year;

    // Public default constructor: uses placeholder make/model.

    public Car() {

        this("Unknown", "Unknown");

    }

    // Public parameterized constructor: sets make/model and defaults year.

    public Car(String make, String model) {

        this(make, model, 2026); // Chained to the full constructor

    }

    // Private full constructor: sets all fields.

    private Car(String make, String model, int year) {

        this.make = make;

        this.model = model;

        this.year = year;

    }

}

It will be 0 because int fields default to 0

It will be 2026 after the chained constructor runs

It will be uninitialized because the constructor is private

It will be the current year determined at runtime

Explanation

This question tests understanding of Java constructors in AP Computer Science A, focusing on tracing values through constructor chains (AP CSA standard). When constructors chain using this(), parameters are passed through the chain until reaching the constructor that performs actual field initialization. Understanding how values flow through constructor chains is crucial for predicting object state after construction. In the Car class, new Car("Honda", "Civic") calls the two-parameter constructor, which chains to the private three-parameter constructor with this("Honda", "Civic", 2026). The private constructor sets year to the hardcoded value 2026. Choice B is correct because the constructor chain explicitly passes 2026 as the year value to the private constructor. Choice C is incorrect because 2026 is a hardcoded literal value, not determined at runtime. To help students: Trace parameter values through each constructor call. Highlight where literal values versus parameters are used. Practice following constructor chains with different arguments. Watch for: assuming default values when constructors provide explicit ones, misunderstanding hardcoded values as dynamic.

9

In the Book code below, how does constructor chaining work in the provided example?


public class Book {

    private String title;

    private String author;

    // Public default constructor: provides standard defaults

    public Book() {

        this("Untitled", "Unknown"); // Chain to parameterized constructor

    }

    // Public parameterized constructor: initializes both fields

    public Book(String title, String author) {

        this.title = title;

        this.author = author;

    }

    // Private constructor: internal helper for author-only

    private Book(String author) {

        this("Untitled", author); // Chain to parameterized constructor

    }

}

this(...) can appear after field assignments in a constructor.

this(...) creates a new Book object inside the constructor.

this(...) calls a constructor in the superclass automatically.

this(...) calls another constructor in the same class.

Explanation

This question tests understanding of Java constructors in AP Computer Science A, focusing on the this() mechanism for constructor chaining. The this(...) syntax is used to call another constructor within the same class, allowing code reuse and ensuring consistent initialization across different constructor variations. In the provided Book code, both the default constructor and the private constructor use this(...) to chain to the two-parameter constructor, passing appropriate values for title and author. Choice B is correct because it accurately describes that this(...) calls another constructor in the same class, enabling constructor chaining. Choice A is incorrect because this(...) doesn't create a new object - it delegates initialization to another constructor for the current object being created. To help students: Demonstrate the difference between this(...) for constructor chaining and new for object creation, using step-by-step execution traces. Watch for: confusion between constructor chaining within a class and inheritance-related super() calls.

10

In the Car code below, what value will the field year have after executing new Car()?


public class Car {

    private String make;

    private String model;

    private int year;

    // Public default constructor: uses the current year

    public Car() {

        this("Unknown", "Unknown", 2026); // Chain to parameterized constructor

    }

    // Public parameterized constructor: initializes all fields

    public Car(String make, String model, int year) {

        this.make = make;

        this.model = model;

        this.year = year;

    }

    // Private constructor: internal helper for known make/model

    private Car(String make, String model) {

        this(make, model, 2026);

    }

}

year is "2026" because it is stored as a String.

year is 2026 after the chained constructor call.

year is 0 because int fields default to zero.

year is uninitialized because the default constructor has no parameters.

Explanation

This question tests understanding of Java constructors in AP Computer Science A, focusing on constructor chaining and value assignment. When new Car() is executed, it invokes the no-argument constructor which immediately chains to the three-parameter constructor using this("Unknown", "Unknown", 2026). In the provided Car code, the chained call passes 2026 as the year parameter, which is then assigned to the year instance field in the three-parameter constructor. Choice B is correct because the constructor chain results in year being set to 2026, the value explicitly passed in the chaining call. Choice A is incorrect because while int fields do default to 0, the constructor explicitly sets year to 2026, overriding any default. To help students: Trace constructor execution with actual values, showing how parameters flow through the chain to field assignments. Watch for: students forgetting that explicit assignments in constructors override default field values.

Page 1 of 2