1 of 10

lasacs.com/ask/506

ASK

2 of 10

lasacs.com/ask/505

ASK

3 of 10

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5

How many non-constructor methods can be invoked by a SavingsAccount object?

class BankAccount {private double balance;public BankAccount()

{ balance = 0; }public BankAccount(double acctBalance) � { balance = acctBalance; }public void deposit(double amount) � { balance += amount; }public void withdraw(double amount)� { balance -= amount; }public double getBalance() {� { return balance; }}

public class SavingsAccount extends BankAccount {private double interestRate;public SavingsAccount() {� { /* implementation not shown */ } � public SavingsAccount(double acctBalance, double rate)

{ /* implementation not shown */ }

public void addInterest() //Add interest to balance� { /* 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 acctBalance){ /* implementation not shown */ } /* FEE of $2 deducted if withdrawal leaves balance less * than MIN_BALANCE. Allows for negative balance. */public void withdraw(double amount){ /* implementation not shown */ } }

Q1

ASK

4 of 10

  • II
  • I & II
  • II & III
  • III
  • I, II, III

Which correctly implements the default constructor of the SavingsAccount class?

I interestRate = 0;super();II super();� interestRate = 0;III super();

class BankAccount {private double balance;public BankAccount()

{ balance = 0; }public BankAccount(double acctBalance) � { balance = acctBalance; }public void deposit(double amount) � { balance += amount; }public void withdraw(double amount)� { balance -= amount; }public double getBalance() {� { return balance; }}

public class SavingsAccount extends BankAccount {private double interestRate;public SavingsAccount() {� { /* implementation not shown */ } � public SavingsAccount(double acctBalance, double rate)

{ /* implementation not shown */ }

public void addInterest() //Add interest to balance� { /* 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 acctBalance){ /* implementation not shown */ } /* FEE of $2 deducted if withdrawal leaves balance less * than MIN_BALANCE. Allows for negative balance. */public void withdraw(double amount){ /* implementation not shown */ } }

c

Q2

ASK

5 of 10

class BankAccount {private double balance;public BankAccount()

{ balance = 0; }public BankAccount(double acctBalance) � { balance = acctBalance; }public void deposit(double amount) � { balance += amount; }public void withdraw(double amount)� { balance -= amount; }public double getBalance() {� { return balance; }}

  • balance = acctBalance;

interestRate = rate;

  • getBalance() = acctBalance;

interestRate = rate;

  • super();

interestRate = rate;

  • super(acctBalance);

interestRate = rate;

  • super(acctBalance,rate);

public class SavingsAccount extends BankAccount {private double interestRate;public SavingsAccount() {� { /* implementation not shown */ } � public SavingsAccount(double acctBalance, double rate)

{ /* implementation not shown */ }

public void addInterest() //Add interest to balance� { /* 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 acctBalance){ /* implementation not shown */ } /* FEE of $2 deducted if withdrawal leaves balance less * than MIN_BALANCE. Allows for negative balance. */public void withdraw(double amount){ /* implementation not shown */ } }

Which is the correct implementation of the initialization constructor in the SavingsAcccount class?

Q3

ASK

6 of 10

  • I
  • II
  • III
  • II & III
  • I, II, III

Which implements the CheckingAccount constructor correctly?

I super(acctBalance);II super();� deposit(acctBalance);�III deposit(acctBalance);

class BankAccount {private double balance;public BankAccount()

{ balance = 0; }public BankAccount(double acctBalance) � { balance = acctBalance; }public void deposit(double amount) � { balance += amount; }public void withdraw(double amount)� { balance -= amount; }public double getBalance() {� { return balance; }}

public class SavingsAccount extends BankAccount {private double interestRate;public SavingsAccount() {� { /* implementation not shown */ } � public SavingsAccount(double acctBalance, double rate)

{ /* implementation not shown */ }

public void addInterest() //Add interest to balance� { /* 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 acctBalance){ /* implementation not shown */ } /* FEE of $2 deducted if withdrawal leaves balance less * than MIN_BALANCE. Allows for negative balance. */public void withdraw(double amount){ /* implementation not shown */ } }

Q4

ASK

7 of 10

Correct withdraw() implementation in CheckingAccount?

A. super.withdraw(amount); D. withdraw(amount);if (balance < MIN_BALANCE) if (getBalance() < MIN_BALANCE)super.withdraw(FEE); withdraw(FEE);�B. withdraw(amount); E. balance -= amount;if (balance < MIN_BALANCE) if (balance < MIN_BALANCE)� withdraw(FEE) balance -= FEE;�C. super.withdraw(amount);if (getBalance() < MIN_BALANCE)super.withdraw(FEE);

class BankAccount {private double balance;public BankAccount()

{ balance = 0; }public BankAccount(double acctBalance) � { balance = acctBalance; }public void deposit(double amount) � { balance += amount; }public void withdraw(double amount)� { balance -= amount; }public double getBalance() {� { return balance; }}

public class SavingsAccount extends BankAccount {private double interestRate;public SavingsAccount() {� { /* implementation not shown */ } � public SavingsAccount(double acctBalance, double rate)

{ /* implementation not shown */ }

public void addInterest() //Add interest to balance� { /* 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 acctBalance){ /* implementation not shown */ } /* FEE of $2 deducted if withdrawal leaves balance less * than MIN_BALANCE. Allows for negative balance. */public void withdraw(double amount){ /* implementation not shown */ } }

Q5

ASK

8 of 10

  • method overloading
  • method overriding
  • downcasting
  • dynamic (late) binding
  • static (early) binding

Redefining the withdraw() method in the CheckingAccount class is an example of

class BankAccount {private double balance;public BankAccount()

{ balance = 0; }public BankAccount(double acctBalance) � { balance = acctBalance; }public void deposit(double amount) � { balance += amount; }public void withdraw(double amount)� { balance -= amount; }public double getBalance() {� { return balance; }}

public class SavingsAccount extends BankAccount {private double interestRate;public SavingsAccount() {� { /* implementation not shown */ } � public SavingsAccount(double acctBalance, double rate)

{ /* implementation not shown */ }

public void addInterest() //Add interest to balance� { /* 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 acctBalance){ /* implementation not shown */ } /* FEE of $2 deducted if withdrawal leaves balance less * than MIN_BALANCE. Allows for negative balance. */public void withdraw(double amount){ /* implementation not shown */ } }

Q6

ASK

9 of 10

  • b.deposit(200);
  • s.withdraw(500);
  • c.withdraw(500);
  • s.deposit(10000);
  • s.addInterest();

Which will cause an error?

class BankAccount {private double balance;public BankAccount()

{ balance = 0; }public BankAccount(double acctBalance) � { balance = acctBalance; }public void deposit(double amount) � { balance += amount; }public void withdraw(double amount)� { balance -= amount; }public double getBalance() {� { return balance; }}

public class SavingsAccount extends BankAccount {private double interestRate;public SavingsAccount() {� { /* implementation not shown */ } � public SavingsAccount(double acctBalance, double rate)

{ /* implementation not shown */ }

public void addInterest() //Add interest to balance� { /* 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 acctBalance){ /* implementation not shown */ } /* FEE of $2 deducted if withdrawal leaves balance less * than MIN_BALANCE. Allows for negative balance. */public void withdraw(double amount){ /* implementation not shown */ } }

BankAccount b = new BankAccount(1400);�BankAccount s = new SavingsAccount(1000,0.04);�BankAccount c = new CheckingAccount(500);

Q7

ASK

10 of 10

  • a SavingsAccount or CheckingAccount constructor
  • addInterest() C. deposit()
  • withdraw() E. getBalance()

To test polymorphism you must use this method

class BankAccount {private double balance;public BankAccount()

{ balance = 0; }public BankAccount(double acctBalance) � { balance = acctBalance; }public void deposit(double amount) � { balance += amount; }public void withdraw(double amount)� { balance -= amount; }public double getBalance() {� { return balance; }}

public class SavingsAccount extends Bank Account {private double interestRate;public SavingsAccount() {� { /* implementation not shown */ } � public SavingsAccount(double acctBalance, double rate)

{ /* implementation not shown */ }

public void addInterest() //Add interest to balance� { /* 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 acctBalance){ /* implementation not shown */ } /* FEE of $2 deducted if withdrawal leaves balance less * than MIN_BALANCE. Allows for negative balance. */public void withdraw(double amount){ /* implementation not shown */ } }

BankAccount b = new BankAccount(1400);�BankAccount s = new SavingsAccount(1000,0.04);�BankAccount c = new CheckingAccount(500);

ASK