Unit 2: Object Orientated Programming Flashcards

1
Q

What are the 3 main characteristics of an object?

A

State (attributes), Behavior (methods), Identity.

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 blueprint for creating objects, defining fields and methods.

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

What is the relationship between a class and an object?

A

An object is an instance of a class.

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

Give two attributes and one method of a Circle class.

A

Attributes: radius, x; Method: area()

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

What are instance variables?

A

Fields that store object-specific data.

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

Why are methods used in a class?

A

To define actions/behaviors that objects can perform.

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

What is a constructor in Java?

A

A special method to initialize an object, named after the class.

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

How do you instantiate an object in Java?

A

ClassName obj = new ClassName();

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

What is the role of the new keyword?

A

Allocates memory and calls the constructor.

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

How do you call a method on an object?

A

objectName.methodName()

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

What is garbage collection in Java?

A

Automatic removal of unreferenced objects from memory.

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

What does the private and public keyword do in Java?

A
  • private: It restricts access to a variable or method to within the same class only.
  • public: It allows access to a method or variable from any other class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is encapsulation in Java?

A

The practice of keeping fields private and accessing them through public methods.

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

Why is encapsulation important?

A

t hides internal details, protects data, and allows control over how data is accessed or modified.

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

What is a getter method?

A

A public method that returns the value of a private field.

17
Q

What is a setter method?

A

A public method that updates the value of a private field.

18
Q

Write a simple getter for a private name field.

A

public String getName() {
return name;
}

19
Q

What are the benefits of using getters and setters?

A

They provide controlled access, validation, and flexibility to change implementation.