Unit 5 Flashcards

1
Q

What is a class in Java?

A

A blueprint for creating objects; defines fields and methods.

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

What is an instance variable?

A

A variable defined in a class for which each object has its own copy.

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

Where are instance variables declared?

A

Inside the class

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

What is a constructor?

A

A special method used to initialize new objects of a class.

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

How do you define a constructor?

A

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 does the new keyword do?

A

It creates a new object and calls a constructor.

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

Can a class have more than one constructor?

A

Yes

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

What is constructor overloading?

A

Having multiple constructors with different parameters.

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

What happens if no constructor is written?

A

Java provides a default no-argument constructor.

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

What is an accessor method?

A

A method that returns the value of a private field (getter).

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

How do you write an accessor method?

A

public int getValue() { return value; }

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

What is a mutator method?

A

A method that modifies the value of a field (setter).

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

How do you write a mutator method?

A

public void setValue(int newVal) { value = newVal; }

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

What is the purpose of the this keyword?

A

It refers to the current object

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

What is scope in Java?

A

The region of code where a variable can be accessed.

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

What is local scope?

A

A variable declared inside a method; only accessible in that method.

17
Q

What is instance scope?

A

A variable declared at the class level; accessible in all instance methods.

18
Q

Can local variables have the same name as instance variables?

19
Q

What is a static variable?

A

A variable shared among all instances of a class.

20
Q

What is a static method?

A

A method that belongs to the class rather than an object.

21
Q

How do you call a static method?

A

Use the class name: ClassName.methodName();

22
Q

Can static methods access instance variables?

23
Q

What is method overloading?

A

Creating multiple methods with the same name but different parameter lists.

24
Q

What is encapsulation?

A

The practice of keeping fields private and providing access via methods.

25
Why make instance variables private?
To protect the data and control how it’s modified.