c) Class
b) To initialize an object’s state
c) private
b) When an object of the class is instantiated
d) Static variable
Animal. Which of these is a correct way to instantiate an object of this class?a) Animal dog = new Animal();
Person has a private attribute age, which of these method signatures is an accessor for age?c) public int getAge() { … }
balance in a class BankAccount?c) public void setBalance(double balance) { … }
b) extends
b) Encapsulation
b) Overriding
Account, if there is a method to withdraw money, which of these is a valid method signature considering best practices?a) public void withdraw(double amount) { … }
a) this
Rectangle that has methods setLength and setWidth. What are these methods collectively known as?b) Mutators
c) Polymorphism
Person class was shown. If a Person object has not yet been instantiated, what is the Person variable’s value in Java?b) null
```java
javaCopy code
Account myAccount = new Account(“John Doe”, 12345, 100.0);
System.out.println(myAccount.deposit(50.0));
~~~
Assuming the deposit method adds the amount to the balance and returns the new balance.
- a) 50.0
- b) 100.0
- c) 150.0
- d) None of the above
c) 150.0
What is a practical example where you wouldn’t write a mutator method for a class attribute?
An example where a mutator method may not be written could be for a final or constant attribute that should not change after the object is created, such as a birthdate or a user ID.
In the context of testing classes and methods in Java, what is the role of a Driver class?
A Driver class is used to test the functionality of other classes. It contains the main method and creates instances of other classes to verify that their methods and interactions work as intended.
Looking at the Bank Account example, identify an improvement that could be made to the Account class related to method design and robustness.
An improvement to the Account class could include adding formal accessors and mutators for all data, as well as improving the design of methods like withdraw to ensure that input values are validated properly, such as checking that the amount to withdraw is positive.