Single Responsibility Principle Flashcards

(24 cards)

1
Q

What is the Single Responsibility Principle (SRP)?

A

The Single Responsibility Principle states that a class should have only one reason to change, meaning it should only have one job or responsibility.

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

True or False: According to SRP, a class can have multiple responsibilities.

A

False

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

Fill in the blank: SRP is one of the five principles of ________.

A

SOLID

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

What are the benefits of adhering to the Single Responsibility Principle?

A

Increased maintainability, easier testing, and better separation of concerns.

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

Which of the following is NOT a consequence of violating SRP? A) Increased complexity B) Easier debugging C) Difficulties in testing

A

B) Easier debugging

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

Short Answer: How does SRP relate to code maintainability?

A

SRP improves code maintainability by ensuring that changes to one responsibility do not affect other parts of the code.

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

What is a common sign that a class violates SRP?

A

If a class has multiple methods that handle unrelated tasks or functionalities.

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

Multiple Choice: Which of the following examples violates SRP? A) A class that handles user authentication B) A class that manages user data and sends notifications

A

B) A class that manages user data and sends notifications

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

True or False: SRP applies only to classes and not to functions or modules.

A

False

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

Fill in the blank: Classes that follow SRP are typically easier to ________.

A

test

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

What is the relationship between SRP and refactoring?

A

Refactoring often involves breaking down classes that violate SRP into smaller, more focused classes.

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

Multiple Choice: Which principle complements SRP by promoting loose coupling? A) Open/Closed Principle B) Interface Segregation Principle C) Dependency Inversion Principle

A

B) Interface Segregation Principle

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

What does it mean for a class to have a ‘single reason to change’?

A

It means that any change in requirements should only necessitate changes in one class, not multiple classes.

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

True or False: SRP can lead to increased code duplication.

A

True

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

Short Answer: How can SRP be implemented in a project?

A

By analyzing class responsibilities and ensuring each class focuses on a single task or functionality.

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

What is a practical example of SRP in software design?

A

A class that handles database operations should not also handle user interface logic.

17
Q

Multiple Choice: Which of the following best describes a class adhering to SRP? A) A class managing user input and database queries B) A class only managing user input

A

B) A class only managing user input

18
Q

Fill in the blank: SRP encourages developers to create ________ classes.

19
Q

What is the impact of SRP on unit testing?

A

SRP makes unit testing easier because each class can be tested in isolation.

20
Q

True or False: SRP is only relevant in object-oriented programming.

21
Q

Short Answer: Name one technique to identify violations of SRP in your code.

A

Code reviews or static code analysis tools.

22
Q

What is a common pitfall when applying SRP?

A

Over-engineering by creating too many small classes for simple functionalities.

23
Q

Multiple Choice: When should SRP be considered during development? A) Only during the design phase B) Throughout the entire development process

A

B) Throughout the entire development process

24
Q

Fill in the blank: The Single Responsibility Principle is primarily concerned with ________ in software design.

A

responsibility