Chapter 8 Flashcards

1
Q

What does each instance of a class have in Java?

A

Its own copy of instance variables.
Example:
Each Rectangle object has its own length and width.

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

What are instance methods, and how are they used?

A

Instance methods require an object instance to be created. They typically interact with instance fields or calculate values.

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

What is a static field or method?

A

A static field or method belongs to the class, not any instance, and is accessed using the class name.

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

How do you declare a static field in Java?

A

Place the static keyword between the access specifier and the type.
Example: private static int instanceCount = 0;

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

How are static methods declared and used?

A

Declared using static between the access modifier and return type. They can be called without creating an instance.
Example:

java
Copy code
public static double milesToKilometers(double miles);
Metric.milesToKilometers(1.0);

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

What happens when an object is passed to a method?

A

The reference value (address) is passed, allowing the method to modify the object contents.

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

Can methods return objects?

A

Yes, methods can return references to objects. However, only the object’s address is returned, not a copy

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

What does the toString method do in Java?

A

Converts an object to a string representation, used implicitly in println and string concatenation.

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

How do you compare objects in Java?

A

Use equals() to compare contents. The == operator compares memory addresses.

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

What are the two types of object copying?

A

Reference Copy: Copies the address, not the object.
Deep Copy: Creates a new instance and copies the values.

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

What is object aggregation?

A

Creating an instance of one class as a reference in another, forming a “has-a” relationship.

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

What does the this keyword do?

A

Refers to the current object instance, resolves shadowing, and supports constructor chaining.

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

What is an enum in Java?

A

A predefined list of constants.
Example:

java
Copy code
enum Day { MONDAY, TUESDAY, … }

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

Name three methods available for an enum.

A

toString(): Returns the constant’s name.
ordinal(): Returns the position of the constant.
compareTo(): Compares based on ordinal values.

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

How does Java manage memory for unused objects?

A

The Garbage Collector reclaims memory for objects without references. Use null to release an object.

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

What is the role of the finalize() method in Java?

A

It runs before an object is reclaimed by the Garbage Collector but its execution time isn’t guaranteed.

17
Q

What is collaboration between classes?

A

When one class interacts with another by knowing its methods and how to call them.

18
Q

What are CRC cards?

A

Cards documenting a class’s responsibilities:

Knows: What the class knows (data).
Does: Actions the class performs.