Chapter 7 Flashcards

1
Q

What is an object in Java?

A

An object is an instance of a class that has state (attributes) and behavior (methods).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a class in Java?

A

A class is a blueprint for creating objects. It defines attributes (fields) and behaviors (methods).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you define a class in Java?

A

Using the class keyword:

```java
class Car {
String model;
int year;
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create an object in Java?

A

Using the new keyword:

```java
Car myCar = new Car();
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a constructor in Java?

A

A special method used to initialize objects. It has the same name as the class and no return type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the default constructor in Java?

A

A constructor that takes no arguments and is provided by Java if no other constructor is defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you define a constructor in Java?

A

```java
class Car {
String model;
int year;
Car(String m, int y) {
model = m;
year = y;
}
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is method overloading?

A

Defining multiple methods with the same name but different parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the this keyword in Java?

A

Refers to the current instance of a class, used to differentiate instance variables from parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are instance variables?

A

Variables declared inside a class but outside methods. Each object has its own copy.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are instance methods?

A

Methods that operate on instance variables and belong to an instance of a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is encapsulation?

A

The practice of keeping fields private and providing access via public methods (getters/setters).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are accessor methods?

A

Methods that retrieve the values of private variables (getters).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are mutator methods?

A

Methods that modify the values of private variables (setters).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the static keyword used for?

A

Declares class-level variables and methods that belong to the class rather than instances.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between instance and static variables?

A

Instance variables belong to objects, while static variables belong to the class and are shared.

17
Q

What is method chaining?

A

Calling multiple methods on the same object in a single statement.

18
Q

What is garbage collection in Java?

A

Automatic memory management that removes unused objects to free memory.

19
Q

What is the finalize() method?

A

A method called by the garbage collector before an object is removed from memory.

20
Q

How can you prevent object modification in Java?

A

Use the final keyword to make fields immutable and remove setters.