0%
0 / 15 answered

Constructors Practice Test

15 Questions
Question
1 / 15
Q1

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

    }

}

Question Navigator