Chapter 6 Flashcards

1
Q

What is an object in Java?

A

An object is an entity in memory that performs specific tasks. It can:

Store data (fields).
Perform operations (methods).

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

Name two capabilities of objects in Java.

A

Objects can store data (fields).
Objects can perform operations (methods).

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

What is a class in Java?

A

A class is a blueprint for creating objects. It specifies:

The data an object can hold (fields).
The actions an object can perform (methods).

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

What is an instance of a class?

A

An instance is a specific object created from a class.

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

Give examples of objects you’ve used in Java.

A

Scanner: Reads input from users.
Random: Generates random numbers.
PrintWriter: Writes data to files.

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

What are the fields in a Rectangle class?

A

length: Holds the rectangle’s length.
width: Holds the rectangle’s width.

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

What are the methods in a Rectangle class?

A

setLength: Sets the length.
setWidth: Sets the width.
getLength: Returns the length.
getWidth: Returns the width.
getArea: Calculates and returns the area (length * width).

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

What does UML stand for?

A

Unified Modeling Language. It is used to visually represent object-oriented systems.

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

What are access specifiers in Java?

A

Java keywords that control how fields or methods can be accessed.

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

What does the public access specifier do?

A

A public member can be accessed by any code, inside or outside the class.

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

What does the private access specifier do?

A

A private member can only be accessed by methods within the same class, protecting it from external access.

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

What is the purpose of the set and get methods in a class?

A

set methods assign values to fields.
get methods retrieve values from fields, ensuring controlled access.

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

What does an object need to perform its actions?

A

An object needs a class as a blueprint to define its fields and methods.

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

How many objects can be created from a single class?

A

As many as needed; each object is an independent instance of the class.

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

Why are access specifiers important?

A

They help encapsulate and protect data within a class, controlling how it can be accessed and modified.

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

What is Data Hiding?

A

Data hiding means fields in a class are private and cannot be directly accessed outside the class.
Access is controlled via public methods (accessors and mutators).

17
Q

What is an Accessor?

A

A method that allows acess to the value of a private field.
Example: public double getLength()

18
Q

What is a Mutator?

A

A method that modifies the value of a private field.
Example: public void setLength(double len)

19
Q

How does Data Hiding ensure integrity?

A

Only class methods can modify an object’s private fields, reducing unintended changes.
External code must use public methods, ensuring controlled updates.

20
Q

What is the purpose of dynamic calculations in methods?

A

Avoids stale data by calculating values (e.g., length × width) dynamically in a method.
Example:
java
Copy code
public double getArea() {
return length * width;
}

21
Q

What is UML?

A

Unified Modeling Language (UML) provides language-independent diagrams for:
Fields
Methods
Access modifiers
Return types

22
Q

What are Instance Fields and Methods?

A

Instance Fields: Each object gets its own copy of these fields.
Instance Methods: Operate on instance fields and require an object to be created.

23
Q

What is a Constructor?

A

A special method that initializes fields and is automatically called when an object is created.
Example:
java
Copy code
public Rectangle(double len, double w) {
length = len;
width = w;
}

24
Q

What is a Default Constructor?

A

Provided by Java if no constructor is written.
Sets numeric fields to 0, boolean fields to false, and object references to null.

25
Q

What is a No-Arg Constructor?

A

A constructor with no parameters.
Example:
java
Copy code
public Rectangle() {
length = 1.0;
width = 1.0;
}

26
Q

What happens if a reference variable is uninitialized?

A

It must reference an object before being used, or a compiler error will occur.
Example:
java
Copy code
Rectangle box;
box = new Rectangle(7.0, 14.0);

27
Q

What is Method Overloading?

A

Multiple methods in a class with the same name but different parameter lists.
Example:
java
Copy code
public void add(int a, int b) { }
public void add(String a, String b) { }

28
Q

What is a Method Signature?

A

The method’s name and the data types of its parameters (order matters).
Return type is not part of the signature.

29
Q

What is Shadowing?

A

A local variable in a method has the same name as an instance field, hiding the field’s value.
Shadowing is discouraged to avoid confusion.

30
Q

How are String objects created?

A

Using a constructor:
java
Copy code
String name = new String(“Michael Long”);
Shorthand:
java
Copy code
String name = “Michael Long”;

31
Q

What happens when an object is passed as an argument?

A

The object’s memory address is passed to the parameter.
The receiving method can access and modify the object.

32
Q

What are Explicit and Wildcard Imports?

A

Explicit Import: Imports a specific class.
java
Copy code
import java.util.Scanner;
Wildcard Import: Imports all classes in a package.
java
Copy code
import java.util.*;

33
Q

What are Responsibilities in Class Design?

A

Knowledge: What the class knows (fields).
Behavior: What the class does (methods).

34
Q

What is the Java Default Package?

A

The java.lang package is automatically available in any Java program.

35
Q

What is the purpose of Method Binding?

A

The compiler matches a method call with the correct overloaded method using the method signature.