Bird
0
0

Given this test code:

medium📝 Predict Output Q5 of 15
Testing Fundamentals - Why Software Testing Matters
Given this test code:
def test_average():
    numbers = []
    avg = sum(numbers) / len(numbers) if numbers else 0
    assert avg == 0

What will happen when this test runs?
AThe test passes but avg is None
BThe test fails due to division by zero error
CThe test raises a syntax error
DThe test passes because avg is correctly set to 0
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code logic

    Check if numbers list is empty; if so, avg is set to 0.
  2. Step 2: Evaluate the assertion

    Since numbers is empty, avg = 0, so assert avg == 0 passes.
  3. Final Answer:

    The test passes because avg is correctly set to 0 -> Option D
  4. Quick Check:

    Empty list handled safely with conditional expression [OK]
Quick Trick: Check for empty list before division to avoid errors [OK]
Common Mistakes:
  • Assuming division by zero will occur without checking list length
  • Ignoring conditional expression handling empty list
  • Confusing avg value with None

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Testing Fundamentals Quizzes