ARCS Inheritance, Abstract Classes, and Interfaces Notes
The questions below refer to the BankAccount, SavingsAccount, and CheckingAccount
classes:
public class BankAccount
private double myBalance;
public BankAccount() {
myBalance = 0;
} public BankAccount(double balance)
{ myBalance = balance;
}
public void deposit(double amount) {
myBalance += amount;
}
public void withdraw(double amount) (
myBalance -= amount;
}
public double getBalance() {
return myBalance;
} }
public class SavingsAccount extends BankAccount
private double mylnterestRate;
public SavingsAccount()
{/‘implementation not shown*/}
public SavingsAccount(double balance, double rate) { /‘implementation not shown*/}
public void addinterest ()
{/‘implementation not shown*/} }
public class CheckingAccount extends BankAccount {
private static final double FEE = 2.0;
private static final double MIN_BALANCE = 50.0;
public CheckingAccount(double balance) {/‘implementation not shown*/}
/* FEE of S2 deducted if withdrawal leaves balance less
* than MIN_BALANCE. Allows for negative balance */
public void withdraw(double amount)
{ /‘implementation not shown */}
}
1. [ANS] Of the methods shown, how many different nonconstructor methods can be
invoked bv a Sav lags Account object? (A) 1 (B) 2 (C) 3 (D) 4 (E) 5
2. [ANS] Vhich of the following correctly implements the default constructor of
the SavingsAccount class? I mylnterestRate ■ 0; super(); II super(); mylnterestRate
• 0; III superO; (A) II only (B) I and II only (C) II and IB only (D) IB
only (E) I, II, and III
3. [ANS] Which is a correct implementation of the constructor with parameters in
the SavingsAccount class? (A) ayBalance • balance; aylnterestRate ■ rate; (B)
getBalanceO ■ balance; mylnterestRate ■ rate; (C) super(); mylnterestRate
■ rate; (D) super(balance); mylnterestRate ■ rate; (E) super(balance, rate);
4. [ANS] VX'hich is a correct implementation of the CheckingAccount constructor?
I super(balance); 11 super (); deposit(balance); III deposit (balance); (A) I only
(B) II only (C) HI only (D) Il and III only (E) I, U, and III
5. [ANS] Which is correct implementation code for the withdraw method in the
CheckingAccount class? (A) super.withdraw(amount); if (myBalance < MIN.BALANCE)
super.withdraw(FEE); (B) withdraw(amount); if (myBalance < MIN.BALANCE)
withdraw(FEE); (C) super.w i thdraw(amount); if (getBalanceO < MIN.BALANCE)
super.withdraw(FEE); (D) withdraw(amount); if (getBalanceO < MIN.BALANCE)
withdraw(FEE); (E) myBalance -■ amount; if (myBalance < MIN.BALANCE) myBalance
-= FEE;
6. [ANS] Redefining the withdraw method in the CheckingAccount class is an
example of (A) method overloading. (B) method overriding. (C) downcasting. (D)
dynamic binding (late binding). (E) static binding (early binding).
Use the following for questions 7 - 9. A program to test the BankAccount,
SavingsAccount, and CheckingAccount classes has these declarations: BankAccount
b = new BankAccount(1408); BankAccount s = new
SavingsAccount(100O, 0.O4); BankAccount c = new
CheckingAccount(500);
7. [ANS] Which method call will cause an error? (A) b. deposit(200); (B)
s.withdraw(500); (C) c.withdraw(500); (D) s .deposit (10000) ; (E)
s.addinterest();
&. [ANS] In order to test polymorphism, which method must be used in the
program? (A) Either a SavingsAccount constructor or a CheckingAccount
constructor (B) addinterest (C) deposit (D) withdraw (E) getBalance
9. [ANS] Which of the following will not cause a ClassCastExceptionto be thrown?
(A) ((SavingsAccount) b).addinterest(); (B) ((CheckingAccount)
b).withdraw(200); (C) ((CheckingAccount) c).deposit(800); (I)) ((CheckingAccount)
s).withdrav(150); (E) ((SavingsAccount) c).addinterest();
10. [ANS] A new method is added to the BankAccount class. /* Transfer amount from
this BankAccount to another BankAccount. * Precondition: myBalance > amount */
public void transfer(BankAccount another, double amount) t withdraw(amount);
another.deposit(amount); } A program has these declarations: BankAccount b = new
BankAccount(650); SavingsAccount timSavings = new SavingsAccount(1500, 0.03);
CheckingAccount daynasChecking = new CheckingAccount(2000); Which of the following
will transfer money from one account to another without error? I
b.transfer(tinsSavings, 50); II tinsSavings.transfer(daynasChecking, 30); III
daynasChecking.transfer(b, 55); (A) 1 only (B) II only (C) III only (D) I, II,
and HI (E) None