Bird
Raised Fist0
Software Engineeringknowledge~20 mins

Software engineering principles - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Software Engineering Principles Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the Single Responsibility Principle

Which of the following best describes the Single Responsibility Principle (SRP) in software engineering?

AA class should have only one reason to change, meaning it should have only one job or responsibility.
BA class should inherit from multiple classes to reuse code efficiently.
CA class should handle all aspects of a program to reduce dependencies.
DA class should be designed to perform multiple unrelated tasks to improve performance.
Attempts:
2 left
πŸ’‘ Hint

Think about how responsibilities affect the reasons for changing a class.

πŸ“‹ Factual
intermediate
2:00remaining
Identifying the Open/Closed Principle

What does the Open/Closed Principle (OCP) state about software entities like classes or modules?

AThey should be open for both extension and modification.
BThey should be open for modification but closed for extension.
CThey should be closed for both extension and modification.
DThey should be open for extension but closed for modification.
Attempts:
2 left
πŸ’‘ Hint

Consider how new features should be added without changing existing code.

πŸ” Analysis
advanced
2:30remaining
Applying the Liskov Substitution Principle

Given a base class Bird with a method fly(), and a subclass Penguin that cannot fly, which design violates the Liskov Substitution Principle (LSP)?

APenguin overrides <code>fly()</code> to throw an exception because it cannot fly.
BPenguin does not override <code>fly()</code> and inherits the flying behavior.
CPenguin is designed as a separate class not inheriting from Bird.
DPenguin has a separate method <code>swim()</code> and does not override <code>fly()</code>.
Attempts:
2 left
πŸ’‘ Hint

Think about substituting a subclass where the base class is expected.

❓ Comparison
advanced
2:30remaining
Comparing Coupling and Cohesion

Which statement correctly compares coupling and cohesion in software design?

AHigh coupling and high cohesion both improve software maintainability.
BLow coupling and low cohesion are ideal for modular design.
CLow coupling and high cohesion improve modularity and maintainability.
DHigh coupling and low cohesion reduce dependencies and improve clarity.
Attempts:
2 left
πŸ’‘ Hint

Consider how components depend on each other and how focused their tasks are.

❓ Reasoning
expert
3:00remaining
Reasoning about Dependency Inversion Principle

In a system where high-level modules depend directly on low-level modules, which change best applies the Dependency Inversion Principle (DIP)?

ACombine high-level and low-level modules into a single module to reduce dependencies.
BMake both high-level and low-level modules depend on abstractions like interfaces.
CMake low-level modules depend on high-level modules to control behavior.
DRemove all abstractions and let modules communicate directly for simplicity.
Attempts:
2 left
πŸ’‘ Hint

Think about how to reduce direct dependencies between different levels of modules.

Practice

(1/5)
1. Which software engineering principle encourages writing code that does one thing and does it well?
easy
A. Open/Closed Principle
B. Don't Repeat Yourself (DRY)
C. Single Responsibility Principle (SRP)
D. Keep It Simple, Stupid (KISS)

Solution

  1. Step 1: Understand the principle names

    SRP means each module or class should have one responsibility or reason to change.
  2. Step 2: Match the description to the principle

    Writing code that does one thing well matches SRP.
  3. Final Answer:

    Single Responsibility Principle (SRP) -> Option C
  4. Quick Check:

    One responsibility = SRP [OK]
Hint: One job per module means SRP [OK]
Common Mistakes:
  • Confusing SRP with DRY which avoids repetition
  • Thinking KISS means single responsibility
  • Mixing Open/Closed with SRP
2. Which of the following is the correct description of the DRY principle?
easy
A. Avoid duplicating code or logic in multiple places
B. Keep code as simple as possible
C. Design software to be open for extension but closed for modification
D. Each class should have only one reason to change

Solution

  1. Step 1: Recall DRY meaning

    DRY stands for "Don't Repeat Yourself", meaning avoid code duplication.
  2. Step 2: Match option to DRY

    Avoid duplicating code or logic in multiple places says avoid duplicating code or logic, which matches DRY.
  3. Final Answer:

    Avoid duplicating code or logic in multiple places -> Option A
  4. Quick Check:

    DRY = no repetition [OK]
Hint: DRY means no repeated code [OK]
Common Mistakes:
  • Confusing DRY with Open/Closed principle
  • Mixing DRY with KISS which is about simplicity
  • Thinking DRY means single responsibility
3. Consider this code snippet applying the KISS principle:
def calculate_area(radius):
    return 3.14 * radius * radius

area = calculate_area(5)
print(area)

What will be the output?
medium
A. 31.4
B. 78.5
C. Error due to missing import
D. None

Solution

  1. Step 1: Calculate area using formula

    Area = Ο€ * radiusΒ² = 3.14 * 5 * 5 = 3.14 * 25 = 78.5
  2. Step 2: Check code output

    The function returns 78.5 and print outputs it.
  3. Final Answer:

    78.5 -> Option B
  4. Quick Check:

    3.14 * 25 = 78.5 [OK]
Hint: Multiply Ο€ by radius squared for area [OK]
Common Mistakes:
  • Using 2 * Ο€ * r (circumference) instead of area formula
  • Expecting error due to missing math module (not needed here)
  • Confusing output with None from missing return
4. Identify the error in this code violating the Open/Closed Principle:
class Shape:
    def area(self, shape_type, size):
        if shape_type == 'circle':
            return 3.14 * size * size
        elif shape_type == 'square':
            return size * size
        else:
            return 0
medium
A. The class is closed for modification
B. The method is open for extension without changes
C. The code uses inheritance properly
D. The area method uses conditional logic to handle multiple shapes

Solution

  1. Step 1: Understand Open/Closed Principle

    It says software entities should be open for extension but closed for modification.
  2. Step 2: Analyze the area method

    The method uses if-else to handle different shapes, so adding a new shape requires modifying this method.
  3. Final Answer:

    The area method uses conditional logic to handle multiple shapes -> Option D
  4. Quick Check:

    Conditional logic breaks Open/Closed [OK]
Hint: If-else for types breaks Open/Closed [OK]
Common Mistakes:
  • Thinking inheritance is used properly here
  • Believing the class is closed for modification
  • Confusing open for extension with modifying code
5. You want to refactor this code to follow the Single Responsibility Principle (SRP):
class User:
    def __init__(self, name, email):
        self.name = name
        self.email = email
    def save_to_database(self):
        # code to save user data
        pass
    def send_welcome_email(self):
        # code to send email
        pass

What is the best way to apply SRP here?
hard
A. Move save_to_database to a separate DatabaseHandler class
B. Keep all methods in User class since they relate to user
C. Combine both methods into one method for simplicity
D. Remove send_welcome_email method entirely

Solution

  1. Step 1: Understand SRP

    SRP says a class should have only one reason to change, so User should only hold user data.
  2. Step 2: Identify responsibilities

    Saving to database and sending emails are separate concerns from user data storage.
  3. Step 3: Refactor accordingly

    Move save_to_database to a DatabaseHandler class to separate concerns.
  4. Final Answer:

    Move save_to_database to a separate DatabaseHandler class -> Option A
  5. Quick Check:

    Separate responsibilities = SRP applied [OK]
Hint: One class, one reason to change means separate concerns [OK]
Common Mistakes:
  • Keeping all methods in one class breaks SRP
  • Combining methods increases complexity
  • Removing functionality instead of refactoring
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard