0%
0 / 7 answered

Methods: Passing and Returning References Practice Test

7 Questions
Question
1 / 7
Q1

Based on the code snippet above, which describes the state of acct after depositAndReturn executes?


public class BankAccount {

    private String accountNumber;

    private double balance;

    public BankAccount(String accountNumber, double balance) {

        this.accountNumber = accountNumber;

        this.balance = balance;

    }

    // Adds funds to this account

    public void deposit(double amount) {

        balance += amount;

    }

    // Deposits then returns this same account reference

    public BankAccount depositAndReturn(double amount) {

        deposit(amount);

        return this;

    }

    public double getBalance() {

        return balance;

    }

    public static void main(String[] args) {

        BankAccount acct = new BankAccount("A200", 100.0);

        BankAccount ref = acct.depositAndReturn(40.0);

        ref.deposit(10.0);

        System.out.println(acct.getBalance());

    }

}

Question Navigator