Bird
Raised Fist0
Software Engineeringknowledge~20 mins

DRY (Don't Repeat Yourself) in Software Engineering - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DRY Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the core idea of DRY

What is the main purpose of the DRY principle in software development?

ATo repeat code in different places to make debugging easier.
BTo write as many lines of code as possible for clarity.
CTo avoid writing the same code or logic multiple times by reusing it.
DTo create multiple copies of code for backup purposes.
Attempts:
2 left
💡 Hint

Think about how repeating code affects maintenance and errors.

🚀 Application
intermediate
2:00remaining
Applying DRY to reduce code duplication

You have two functions that calculate the area of a rectangle and a square separately, but both use the same multiplication logic. How can you apply DRY to improve this?

AKeep both functions separate to avoid confusion.
BCreate a single function that takes length and width as parameters and use it for both shapes.
CCopy the multiplication logic into a third function and call it inside both functions.
DWrite the multiplication logic directly inside the main program instead of functions.
Attempts:
2 left
💡 Hint

Think about how one function can handle both shapes by using parameters.

🔍 Analysis
advanced
2:00remaining
Identifying DRY violations in code

Which of the following code snippets violates the DRY principle the most?

Software Engineering
def greet_morning():
    print('Good morning!')

def greet_evening():
    print('Good evening!')

def greet_night():
    print('Good night!')
AEach function repeats the print statement with only the greeting text changed.
BAll functions use a single print statement with different parameters.
CFunctions are combined into one with a parameter for the greeting.
DThere is no repetition; each function is unique.
Attempts:
2 left
💡 Hint

Look for repeated code blocks that differ only slightly.

❓ Comparison
advanced
2:00remaining
Comparing DRY with WET (Write Everything Twice)

Which statement best explains the difference between DRY and WET principles?

ADRY encourages code reuse to avoid duplication, while WET accepts duplication and repeats code.
BDRY means writing code twice for safety, WET means writing code once.
CDRY and WET are the same principles with different names.
DWET is a modern replacement for DRY focusing on code repetition.
Attempts:
2 left
💡 Hint

Think about what each acronym stands for and their goals.

❓ Reasoning
expert
3:00remaining
Reasoning about DRY in large projects

In a large software project, which of the following is a potential downside of strictly applying DRY everywhere without careful design?

AIncreased number of bugs due to code duplication.
BHaving too many repeated code blocks making the project larger.
CSlower program execution because of repeated code.
DOverly complex shared code that is hard to understand and maintain.
Attempts:
2 left
💡 Hint

Consider what happens if you try to reuse code too much without clear structure.

Practice

(1/5)
1. What does the DRY principle in software engineering mean?
easy
A. Don't Repeat Yourself - avoid writing the same code multiple times
B. Debug Regularly Yourself - test code often
C. Develop Rapidly Yesterday - finish tasks quickly
D. Design Reusable Yields - focus on output

Solution

  1. Step 1: Understand the acronym DRY

    DRY stands for "Don't Repeat Yourself" which means avoiding duplicate code.
  2. Step 2: Match the meaning to options

    Don't Repeat Yourself - avoid writing the same code multiple times correctly explains DRY as avoiding repeated code to keep it clean and maintainable.
  3. Final Answer:

    Don't Repeat Yourself - avoid writing the same code multiple times -> Option A
  4. Quick Check:

    DRY = Don't Repeat Yourself [OK]
Hint: DRY means write code once, reuse it everywhere [OK]
Common Mistakes:
  • Confusing DRY with debugging or speed
  • Thinking DRY means writing less code overall
  • Assuming DRY is about output or design only
2. Which of the following is the correct way to apply DRY in code?
easy
A. Use functions or methods to reuse code
B. Copy and paste the same code block multiple times
C. Write all code in one long script without breaks
D. Repeat variable declarations for clarity

Solution

  1. Step 1: Identify how to avoid code repetition

    Functions or methods allow reusing code by calling them multiple times instead of copying code.
  2. Step 2: Compare options for DRY compliance

    Use functions or methods to reuse code uses functions to reuse code, which follows DRY. Others increase repetition.
  3. Final Answer:

    Use functions or methods to reuse code -> Option A
  4. Quick Check:

    Functions help reuse code [OK]
Hint: Functions help avoid repeating code [OK]
Common Mistakes:
  • Copy-pasting code instead of using functions
  • Writing long scripts without modular parts
  • Repeating declarations unnecessarily
3. Consider this code snippet:
def greet():
    print('Hello!')

greet()
greet()

What is the main benefit of using the function greet() here?
medium
A. It causes a syntax error
B. It repeats the print statement twice
C. It makes the program slower
D. It avoids repeating the print code by calling the function

Solution

  1. Step 1: Analyze the function usage

    The function greet() contains the print statement and is called twice.
  2. Step 2: Understand the benefit

    Instead of writing print('Hello!') twice, the function is called twice, avoiding code repetition.
  3. Final Answer:

    It avoids repeating the print code by calling the function -> Option D
  4. Quick Check:

    Function calls reuse code [OK]
Hint: Functions let you write code once, run many times [OK]
Common Mistakes:
  • Thinking function calls duplicate code
  • Assuming functions slow down simple prints
  • Believing this causes syntax errors
4. Identify the problem in this code snippet related to DRY:
def calculate_area(radius):
    area = 3.14 * radius * radius
    return area

print(calculate_area(5))
print(3.14 * 7 * 7)
medium
A. The value of pi is incorrect
B. The function is not called for the second print, repeating code
C. The function has a syntax error
D. The function returns nothing

Solution

  1. Step 1: Check code repetition

    The first print uses the function, but the second print repeats the area calculation manually.
  2. Step 2: Identify DRY violation

    Repeating the formula outside the function breaks DRY; both should use the function.
  3. Final Answer:

    The function is not called for the second print, repeating code -> Option B
  4. Quick Check:

    Repeated formula outside function breaks DRY [OK]
Hint: Always reuse functions instead of repeating formulas [OK]
Common Mistakes:
  • Ignoring repeated formula outside function
  • Thinking the function has syntax errors
  • Assuming pi value causes DRY issues
5. You have a list of prices and want to apply a 10% discount to each without repeating code. Which approach best follows the DRY principle?
hard
A. Write a loop that calculates discounted price inside the loop body each time
B. Manually calculate discount for each price separately
C. Create a function to calculate discount and call it for each price
D. Copy the discount calculation code multiple times in the program

Solution

  1. Step 1: Understand the task

    Applying the same discount calculation repeatedly should be done without copying code.
  2. Step 2: Choose the DRY-compliant method

    Creating a function for discount calculation and calling it for each price avoids repetition and keeps code clean.
  3. Final Answer:

    Create a function to calculate discount and call it for each price -> Option C
  4. Quick Check:

    Functions prevent repeated discount code [OK]
Hint: Use functions to apply repeated calculations [OK]
Common Mistakes:
  • Repeating calculation code in loops or manually
  • Copy-pasting discount logic multiple times
  • Not using reusable code blocks
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