Unit 2A Flashcards

testtt

1
Q
  1. In Java, which term describes a blueprint from which individual objects are created?
    • a) Method
    • b) Instance
    • c) Class
    • d) Object
A

c) Class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. 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
A

b) To initialize an object’s state

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. 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
A

c) private

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. 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
A

b) When an object of the class is instantiated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. 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
A

d) Static variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. 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

a) Animal dog = new Animal();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. If a class Person has a private attribute age, which of these method signatures is an accessor for age?
    • a) public void getAge() { … }
    • b) private int getAge() { … }
    • c) public int getAge() { … }
    • d) public void setAge(int age) { … }
A

c) public int getAge() { … }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Which method would be used to modify the private attribute balance in a class BankAccount?
    • a) public double getBalance() { … }
    • b) private void updateBalance(double balance) { … }
    • c) public void setBalance(double balance) { … }
    • d) public double checkBalance() { … }
A

c) public void setBalance(double balance) { … }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. 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
A

b) extends

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. 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
A

b) Encapsulation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. 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
A

b) Overriding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. In a class named 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) { … }
    • b) public double withdraw(double amount) { … }
    • c) private void withdraw(double amount) { … }
    • d) public boolean withdraw(double amount) { … }
A

a) public void withdraw(double amount) { … }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. 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

a) this

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. Consider a class Rectangle that has methods setLength and setWidth. What are these methods collectively known as?
    • a) Accessors
    • b) Mutators
    • c) Executors
    • d) Controllers
A

b) Mutators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  1. 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
A

c) Polymorphism

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
  1. n the slides, a Person class was shown. If a Person object has not yet been instantiated, what is the Person variable’s value in Java?
    • a) 0
    • b) null
    • c) undefined
    • d) NaN
A

b) null

17
Q
  1. 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

A

c) 150.0

18
Q

What is a practical example where you wouldn’t write a mutator method for a class attribute?

A

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.

19
Q

In the context of testing classes and methods in Java, what is the role of a Driver class?

A

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.

20
Q

Looking at the Bank Account example, identify an improvement that could be made to the Account class related to method design and robustness.

A

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.