Objects: Instances of Classes

Help Questions

AP Computer Science A › Objects: Instances of Classes

Questions 1 - 7
1

Consider the following Java class and code; after executing it, what is the state of checking's balance?


// Bank account management example

class BankAccount {

    private String owner;

    private double balance;

    public BankAccount(String owner, double startingBalance) {

        this.owner = owner;

        balance = startingBalance;

    }

    public void deposit(double amount) {

        balance += amount;

    }

    public boolean withdraw(double amount) {

        if (amount <= balance) {

            balance -= amount;

            return true;

        }

        return false;

    }

    public double getBalance() {

        return balance;

    }

}

class Main {

    public static void main(String[] args) {

        BankAccount checking = new BankAccount("Ava", 200.0);

        BankAccount savings = new BankAccount("Ben", 500.0);

        checking.deposit(50.0);

        savings.withdraw(100.0);

        checking.withdraw(120.0);

        // (No printing here)

    }

}

```​

The balance is $250.0.

The balance is $200.0.

The balance is $80.0.

The balance is $130.0.

Explanation

This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, the checking account starts with $200.0, then deposit(50.0) adds $50 to make it $250, and withdraw(120.0) subtracts $120, resulting in a final balance of $130. Choice A is correct because it reflects the updated balance after all method calls, showing accurate tracking of state changes through multiple operations. Choice C is incorrect because it only accounts for the deposit, ignoring the withdrawal. To help students: Trace through each method call step-by-step, updating the balance after each operation. Emphasize that each object maintains its own state independently. Watch for: students who only track some operations or confuse the states of different objects.

2

Consider the following Java class and code; what is the output of the following method call?


class BankAccount {

    private String owner;

    private double balance;

    public BankAccount(String owner, double startingBalance) {

        this.owner = owner;

        balance = startingBalance;

    }

    public void deposit(double amount) {

        balance += amount;

    }

    public boolean withdraw(double amount) {

        if (amount <= balance) {

            balance -= amount;

            return true;

        }

        return false;

    }

    public double getBalance() {

        return balance;

    }

}

class Main {

    public static void main(String[] args) {

        BankAccount checking = new BankAccount("Ava", 75.0);

        BankAccount savings = new BankAccount("Ben", 125.0);

        checking.withdraw(80.0);

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

    }

}

```​

-5.0

0.0

75.0

80.0

Explanation

This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, checking has a balance of $75.0 and attempts to withdraw $80.0. Since the withdrawal amount exceeds the balance, the withdraw method's condition fails, and the balance remains unchanged at $75.0. Choice C is correct because the withdraw method protects against overdrafts, maintaining the original balance when the requested amount exceeds available funds. Choice B is incorrect because it assumes the withdrawal succeeds and creates a negative balance, which the method logic prevents. To help students: Reinforce the importance of conditional checks in methods. Practice tracing through failed operations and understanding their effects. Watch for: students who assume all method calls succeed or who incorrectly calculate results of failed operations.

3

Consider the following Java class and code; after executing it, what is the state of checking's balance?


class BankAccount {

    private String owner;

    private double balance;

    public BankAccount(String owner, double startingBalance) {

        this.owner = owner;

        balance = startingBalance;

    }

    public void deposit(double amount) {

        balance += amount;

    }

    public boolean withdraw(double amount) {

        if (amount <= balance) {

            balance -= amount;

            return true;

        }

        return false;

    }

    public void transferTo(BankAccount other, double amount) {

        if (withdraw(amount)) {

            other.deposit(amount);

        }

    }

    public double getBalance() {

        return balance;

    }

}

class Main {

    public static void main(String[] args) {

        BankAccount checking = new BankAccount("Ava", 90.0);

        BankAccount savings = new BankAccount("Ben", 10.0);

        checking.transferTo(savings, 50.0);

        checking.transferTo(savings, 60.0);

    }

}

```​

The balance is $90.0.

The balance is $30.0.

The balance is $40.0.

The balance is $-20.0.

Explanation

This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, checking starts with $90.0, successfully transfers $50.0 to savings (leaving $40.0), then attempts to transfer $60.0 but fails because only $40.0 remains. The second transfer doesn't occur due to insufficient funds, so checking's final balance is $40.0. Choice B is correct because it reflects the balance after one successful transfer and one failed transfer attempt. Choice D is incorrect because it assumes both transfers succeed, ignoring the balance check in the withdraw method. To help students: Practice scenarios with multiple operations where some may fail. Emphasize that each operation depends on the current state. Watch for: students who don't track state changes between operations or assume all transfers succeed.

4

Consider the following Java class and code; what is the effect of the method call checking.deposit(savings.getBalance()) on checking?


class BankAccount {

    private String owner;

    private double balance;

    public BankAccount(String owner, double startingBalance) {

        this.owner = owner;

        balance = startingBalance;

    }

    public void deposit(double amount) {

        balance += amount;

    }

    public boolean withdraw(double amount) {

        if (amount <= balance) {

            balance -= amount;

            return true;

        }

        return false;

    }

    public double getBalance() {

        return balance;

    }

}

class Main {

    public static void main(String[] args) {

        BankAccount checking = new BankAccount("Ava", 20.0);

        BankAccount savings = new BankAccount("Ben", 80.0);

        checking.deposit(savings.getBalance());

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

    }

}

```​

checking prints 20.0 after the call.

checking prints 80.0 after the call.

checking prints 100.0 after the call.

checking prints 60.0 after the call.

Explanation

This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, savings.getBalance() returns 80.0, which is then passed as the argument to checking.deposit(). This adds $80.0 to checking's initial balance of $20.0, resulting in a final balance of $100.0. Choice B is correct because it shows the result of depositing the value returned by one object's method into another object. Choice D is incorrect because it only considers the deposited amount, ignoring checking's initial balance. To help students: Practice method composition where one method's return value becomes another's argument. Emphasize that deposit adds to the existing balance rather than replacing it. Watch for: confusion about whether methods add to or replace values, or misunderstanding method chaining.

5

Consider the following Java class and code; after executing it, what is the state of acct2's balance?


class BankAccount {

    private String owner;

    private double balance;

    public BankAccount(String owner, double startingBalance) {

        this.owner = owner;

        balance = startingBalance;

    }

    public void deposit(double amount) {

        balance += amount;

    }

    public boolean withdraw(double amount) {

        if (amount <= balance) {

            balance -= amount;

            return true;

        }

        return false;

    }

    public double getBalance() {

        return balance;

    }

}

class Main {

    public static void main(String[] args) {

        BankAccount acct1 = new BankAccount("Ava", 100.0);

        BankAccount acct2 = acct1; // two references to the same object

        acct2.deposit(40.0);

        acct1.withdraw(10.0);

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

    }

}

```​

40.0

90.0

100.0

130.0

Explanation

This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, acct2 = acct1 creates two references pointing to the same BankAccount object, not two separate objects. When acct2.deposit(40.0) is called, it modifies the shared object to have balance $140.0, and acct1.withdraw(10.0) further modifies it to $130.0. Choice A is correct because both references point to the same object, so all modifications affect the single shared balance. Choice B is incorrect because it assumes acct1 and acct2 are separate objects with independent balances. To help students: Use memory diagrams to visualize reference variables pointing to objects. Emphasize the difference between creating new objects versus creating new references. Watch for: the common misconception that assignment creates a copy of the object rather than copying the reference.

6

Consider the following Java class and code; after executing it, what is the state of savings's balance?


class BankAccount {

    private String owner;

    private double balance;

    public BankAccount(String owner, double startingBalance) {

        this.owner = owner;

        balance = startingBalance;

    }

    public void deposit(double amount) {

        balance += amount;

    }

    public boolean withdraw(double amount) {

        if (amount <= balance) {

            balance -= amount;

            return true;

        }

        return false;

    }

    // Transfer money between two objects (interaction)

    public void transferTo(BankAccount other, double amount) {

        if (withdraw(amount)) {

            other.deposit(amount);

        }

    }

    public double getBalance() {

        return balance;

    }

}

class Main {

    public static void main(String[] args) {

        BankAccount checking = new BankAccount("Ava", 300.0);

        BankAccount savings = new BankAccount("Ben", 100.0);

        checking.transferTo(savings, 80.0);

        savings.withdraw(50.0);

    }

}

```​

The balance is $180.0.

The balance is $100.0.

The balance is $50.0.

The balance is $130.0.

Explanation

This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, the transferTo method demonstrates object interaction: checking withdraws $80 (if possible) and deposits it into savings. Starting with $100, savings receives $80 to become $180, then withdraws $50, resulting in a final balance of $130. Choice B is correct because it accurately reflects the balance after both the transfer and withdrawal operations. Choice C is incorrect because it only accounts for the transfer, ignoring the subsequent withdrawal. To help students: Trace complex operations involving multiple objects step-by-step. Emphasize how methods can coordinate actions between different object instances. Watch for: students who lose track of operations or don't understand how objects can interact through method calls.

7

Consider the following Java class and code; what is the effect of the method call savings.withdraw(600.0) on savings?


class BankAccount {

    private String owner;

    private double balance;

    public BankAccount(String owner, double startingBalance) {

        this.owner = owner;

        balance = startingBalance;

    }

    public void deposit(double amount) {

        balance += amount;

    }

    public boolean withdraw(double amount) {

        if (amount <= balance) {

            balance -= amount;

            return true;

        }

        return false;

    }

    public double getBalance() {

        return balance;

    }

}

class Main {

    public static void main(String[] args) {

        BankAccount checking = new BankAccount("Ava", 200.0);

        BankAccount savings = new BankAccount("Ben", 500.0);

        savings.withdraw(600.0);

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

    }

}

```​

Balance becomes $600.0 after withdrawal.

Balance remains $500.0 after withdrawal.

Balance becomes $0.0 after withdrawal.

Balance becomes $-100.0 after withdrawal.

Explanation

This question tests AP Computer Science A skills: understanding objects as instances of classes and their interaction through methods. Objects in Java are instances of classes, which define fields and methods; methods allow objects to perform actions or modify state. In this scenario, the withdraw method includes a conditional check: it only processes the withdrawal if the amount is less than or equal to the current balance. Since savings has $500.0 and the withdrawal request is for $600.0, the condition fails and the balance remains unchanged at $500.0. Choice B is correct because the withdraw method's protective logic prevents overdrafts, maintaining the original balance when insufficient funds exist. Choice A is incorrect because it assumes the withdrawal happens regardless of the balance check, which would violate the method's logic. To help students: Emphasize the importance of reading conditional logic in methods. Practice tracing through if-statements with different scenarios. Watch for: students who ignore conditional checks or assume all method calls succeed.