Bird
Raised Fist0
Testing Fundamentalstesting~20 mins

Statement coverage in Testing Fundamentals - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Statement Coverage Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Statement Coverage

Which of the following best describes statement coverage in software testing?

AIt measures the percentage of executable statements in the code that have been executed by the test cases.
BIt measures the percentage of decision outcomes (true/false) tested in the code.
CIt measures the number of test cases executed during testing.
DIt measures the time taken to execute all test cases.
Attempts:
2 left
💡 Hint

Think about what part of the code is actually run during testing.

❓ Predict Output
intermediate
2:00remaining
Calculate Statement Coverage Percentage

Given the following code and test cases, what is the statement coverage percentage?

def check_number(x):
    if x > 0:
        print("Positive")
    else:
        print("Non-positive")

# Test cases:
check_number(5)
A25%
B50%
C100%
D75%
Attempts:
2 left
💡 Hint

Count how many lines run when check_number(5) is called.

❓ assertion
advanced
2:00remaining
Assertion for Full Statement Coverage

Which assertion correctly verifies that a test suite achieves 100% statement coverage for the following function?

def is_even(n):
    if n % 2 == 0:
        return True
    else:
        return False
Aassert statement_coverage(is_even_tests) == 0
Bassert statement_coverage(is_even_tests) < 100
Cassert statement_coverage(is_even_tests) >= 50
Dassert statement_coverage(is_even_tests) == 100
Attempts:
2 left
💡 Hint

Full coverage means all lines run at least once.

🔧 Debug
advanced
2:00remaining
Debugging Statement Coverage Measurement

Consider this test code measuring statement coverage. It reports 80% coverage, but the tester expects 100%. What is the likely cause?

def foo(x):
    if x > 0:
        print("Positive")
    else:
        print("Zero or Negative")

# Test cases
foo(1)
# Coverage measured here
AThe function foo has a syntax error causing incomplete coverage.
BThe print statements cause the coverage tool to fail.
CThe test cases do not cover the else branch, so some statements are not executed.
DThe coverage tool only measures function calls, not statements.
Attempts:
2 left
💡 Hint

Think about which lines run when foo(1) is called.

❓ framework
expert
2:30remaining
Integrating Statement Coverage in Automated Testing

Which option correctly describes how to integrate statement coverage measurement into an automated Python test suite using pytest and coverage.py?

ARun tests with <code>coverage run -m pytest</code> then generate report with <code>coverage report</code>.
BAdd <code>assert coverage == 100</code> inside each test function manually.
CRun tests with <code>pytest --coverage</code> without installing any extra tools.
DUse <code>pytest-cov</code> but coverage reports are generated automatically without any commands.
Attempts:
2 left
💡 Hint

Think about the standard commands to run coverage with pytest.

Practice

(1/5)
1. What does statement coverage measure in software testing?
easy
A. The number of test cases written
B. The number of bugs found in the code
C. The time taken to run all tests
D. The percentage of code lines executed during testing

Solution

  1. Step 1: Understand the definition of statement coverage

    Statement coverage measures how many lines of code are executed when tests run.
  2. Step 2: Compare options to the definition

    Only The percentage of code lines executed during testing correctly describes statement coverage as the percentage of executed code lines.
  3. Final Answer:

    The percentage of code lines executed during testing -> Option D
  4. Quick Check:

    Statement coverage = executed code lines percentage [OK]
Hint: Statement coverage counts executed lines, not tests or bugs [OK]
Common Mistakes:
  • Confusing statement coverage with number of test cases
  • Thinking it measures bugs found
  • Mixing it up with test execution time
2. Which of the following is the correct way to describe statement coverage in a test report?
easy
A. Statement coverage is the number of test cases executed.
B. Statement coverage is the percentage of code statements executed during testing.
C. Statement coverage is the total time taken to run all tests.
D. Statement coverage is the number of bugs found per test.

Solution

  1. Step 1: Identify the correct definition for statement coverage in reports

    Statement coverage is reported as the percentage of code statements executed during tests.
  2. Step 2: Match options to the correct definition

    Statement coverage is the percentage of code statements executed during testing. correctly states this; others confuse coverage with test count, time, or bugs.
  3. Final Answer:

    Statement coverage is the percentage of code statements executed during testing. -> Option B
  4. Quick Check:

    Statement coverage = % of executed statements [OK]
Hint: Coverage means percent of executed code lines, not test counts [OK]
Common Mistakes:
  • Reporting coverage as number of tests
  • Confusing coverage with test duration
  • Mixing coverage with bug counts
3. Consider this code snippet:
if (x > 0) {
  print("Positive");
} else {
  print("Non-positive");
}

If a test runs with x = 5, what is the statement coverage?
medium
A. 100% because all lines run
B. 75% because some lines run
C. 50% because only the if block runs
D. 0% because no else block runs

Solution

  1. Step 1: Count total statements in the code

    There are 4 statements: the if condition, print("Positive"), else, print("Non-positive").
  2. Step 2: Identify executed statements when x=5

    The if condition and print("Positive") run; else and its print do not run. So 2 out of 4 statements run.
  3. Final Answer:

    50% because only the if block runs -> Option C
  4. Quick Check:

    Executed statements = 2/4 = 50% [OK]
Hint: Only executed lines count; else block skipped means less coverage [OK]
Common Mistakes:
  • Assuming else block runs anyway
  • Counting conditions as multiple statements
  • Thinking coverage is always 100%
4. You have this code:
if (a == 10) {
  print("Ten");
}
print("Done");

A test runs with a = 5 but reports 100% statement coverage. What is wrong?
medium
A. The test did not execute the if block, so coverage cannot be 100%
B. The print("Done") statement was not executed
C. The test input is invalid
D. The coverage tool counts only executed lines, so 100% is correct

Solution

  1. Step 1: Identify statements and their execution

    There are 3 statements: if condition, print("Ten"), and print("Done"). With a=5, the if block is skipped, so print("Ten") is not executed.
  2. Step 2: Check coverage report validity

    Since one statement is not executed, coverage cannot be 100%. The report is incorrect or the test did not run as expected.
  3. Final Answer:

    The test did not execute the if block, so coverage cannot be 100% -> Option A
  4. Quick Check:

    Skipped statements mean coverage less than 100% [OK]
Hint: Skipped code means coverage less than 100%, check test inputs [OK]
Common Mistakes:
  • Assuming coverage counts unexecuted lines
  • Ignoring skipped if blocks
  • Trusting coverage tool blindly
5. Given this code:
if (score >= 90) {
  grade = 'A';
} else if (score >= 80) {
  grade = 'B';
} else {
  grade = 'C';
}

Which test inputs will achieve 100% statement coverage?
hard
A. score = 95, score = 85, and score = 70
B. score = 95 and score = 85
C. score = 95 only
D. score = 70 only

Solution

  1. Step 1: Identify all statements in the code

    There are 7 statements: 3 conditions and 4 assignments (grade = 'A', grade = 'B', grade = 'C', plus else blocks).
  2. Step 2: Determine inputs to cover all branches

    To cover all statements, tests must run each branch: score=95 covers first if, score=85 covers else if, score=70 covers else.
  3. Final Answer:

    score = 95, score = 85, and score = 70 -> Option A
  4. Quick Check:

    All branches covered by three inputs = 100% coverage [OK]
Hint: Test all branches to cover all statements fully [OK]
Common Mistakes:
  • Testing only some branches
  • Assuming one input covers all
  • Ignoring else branch coverage
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