Unit 2A Flashcards
testtt
1
Q
- In Java, which term describes a blueprint from which individual objects are created?
- a) Method
- b) Instance
- c) Class
- d) Object
A
c) Class
2
Q
- 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
3
Q
- 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
4
Q
- 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
5
Q
- 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
6
Q
- 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();
7
Q
- 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) { … }
A
c) public int getAge() { … }
8
Q
- 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() { … }
A
c) public void setBalance(double balance) { … }
9
Q
- 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
10
Q
- 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
11
Q
- 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
12
Q
- 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
a) public void withdraw(double amount) { … }
13
Q
- 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
14
Q
- Consider a class
Rectangle
that has methodssetLength
andsetWidth
. What are these methods collectively known as?- a) Accessors
- b) Mutators
- c) Executors
- d) Controllers
A
b) Mutators
15
Q
- 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