0%
0 / 7 answered
Objects: Instances of Classes Practice Test
•7 QuestionsQuestion
1 / 7
Q1
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)
}
}
```
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)
}
}
```