Introduction to OOP Flashcards

1
Q

What is the purpose of encapsulation in OOP?

A

Encapsulation protects data by grouping it with related methods into a class, restricting access to internal details. This ensures data integrity and modular, maintainable code.

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

How do you control access to class fields in Java?

A

Use the private modifier for fields and provide public getter and setter methods to access or modify them.

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

What is the difference between a class and an object?

A

A class is a blueprint for objects, defining properties and methods. An object is an instance of a class, storing actual data and behavior.

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

When should you use an interface over an abstract class?

A

Use an interface for defining shared behavior across unrelated classes or when multiple inheritance is needed. Use an abstract class for closely related classes.

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

What is the role of a constructor in Java?

A

A constructor initializes an object’s attributes during creation. It has no return type and must match the class name.

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

What are the main types of constructors in Java?

A

Default Constructor: No parameters, provided automatically if none is defined.
No-Arg Constructor: Explicitly defined with no parameters.
Parameterized Constructor: Takes arguments to set specific object states.

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

How does the static keyword improve memory efficiency?

A

Static variables and methods belong to the class rather than instances, allowing shared memory space and avoiding redundant object creation.

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

What are the four access modifiers in Java, and where are they accessible?

A

Public: Accessible everywhere.
Private: Accessible only within the class.
Protected: Accessible within the same package and subclasses.
Default (Package-Private): Accessible only within the same package.

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

How does abstraction simplify complex systems?

A

Protects data from unauthorized access.
Ensures code modularity and reusability.
Improves maintainability by hiding implementation details.

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

What is the difference between a field and a variable?

A

Field: A data member of a class, used to hold an object’s state.
Variable: A named storage space, which can be local (inside methods) or global (fields).

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