Unit 2A Flashcards
testtt
- In Java, which term describes a blueprint from which individual objects are created?
- a) Method
- b) Instance
- c) Class
- d) Object
c) Class
- What is the purpose of a constructor in a Java class?
- a) To declare variables
- b) To initialize an object’s state
- c) To perform cleanup tasks for an object
- d) To return the instance of a class
b) To initialize an object’s state
- Which of these Java keywords is used to encapsulate the variables of a class for data protection?
- a) static
- b) final
- c) private
- d) protected
c) private
- In Java, when is the default constructor of a class called?
- a) When the class is saved
- b) When an object of the class is instantiated
- c) When the class is compiled
- d) When the class is deleted
b) When an object of the class is instantiated
- What is the term for a variable that belongs to the class rather than any instance, and there’s only one copy of it?
- a) Instance variable
- b) Local variable
- c) Class variable
- d) Static variable
d) Static variable
- Consider a class
Animal
. Which of these is a correct way to instantiate an object of this class?- a) Animal dog = new Animal();
- b) new Animal dog;
- c) Animal = new dog();
- d) new Animal() dog;
a) Animal dog = new Animal();
- If a class
Person
has a private attributeage
, which of these method signatures is an accessor forage
?- a) public void getAge() { … }
- b) private int getAge() { … }
- c) public int getAge() { … }
- d) public void setAge(int age) { … }
c) public int getAge() { … }
- Which method would be used to modify the private attribute
balance
in a classBankAccount
?- a) public double getBalance() { … }
- b) private void updateBalance(double balance) { … }
- c) public void setBalance(double balance) { … }
- d) public double checkBalance() { … }
c) public void setBalance(double balance) { … }
- When creating a subclass in Java, which keyword is used to inherit properties and methods from another class?
- a) implements
- b) extends
- c) inherits
- d) derives
b) extends
- In object-oriented design, what is the concept of bundling data and methods that operate on the data within one unit?
- a) Polymorphism
- b) Encapsulation
- c) Inheritance
- d) Abstraction
b) Encapsulation
- What is the term for a method in a subclass that has the same name and type signature as a method in the superclass?
- a) Overloading
- b) Overriding
- c) Shadowing
- d) Mirroring
b) Overriding
- In a class named
Account
, if there is a method towithdraw
money, which of these is a valid method signature considering best practices?- a) public void withdraw(double amount) { … }
- b) public double withdraw(double amount) { … }
- c) private void withdraw(double amount) { … }
- d) public boolean withdraw(double amount) { … }
a) public void withdraw(double amount) { … }
- What keyword would you use in a method to refer to the current object on which the method is being called?
- a) this
- b) self
- c) me
- d) current
a) this
- Consider a class
Rectangle
that has methodssetLength
andsetWidth
. What are these methods collectively known as?- a) Accessors
- b) Mutators
- c) Executors
- d) Controllers
b) Mutators
- Which principle of object-oriented programming is being used when different classes are designed to have the same method but with different implementations?
- a) Encapsulation
- b) Inheritance
- c) Polymorphism
- d) Composition
c) Polymorphism
- n the slides, a
Person
class was shown. If aPerson
object has not yet been instantiated, what is thePerson
variable’s value in Java?- a) 0
- b) null
- c) undefined
- d) NaN
b) null
- What is the output of the following code snippet?
```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.