Skip to content
Independent guides for QA & test automationRSSEditorial policy
QA Vibes

Practice · Test design

Statement and branch coverage questions and answers

Exam-style statement and branch coverage questions, generated fresh each time, with answers computed to the ISTQB Foundation Level v4.0 definitions (section 4.3.1 and 4.3.2). Every wrong answer tells you which mistake leads to it.

No answers yet

Statement and branch coverage

A function contains this code:

  • 1. retry = 3
  • IF a > 0 THEN
  • 2. status = "review"
  • ELSE
  • 3. total = total + 5
  • END IF
  • IF b > 7 THEN
  • 4. discount = 10
  • ELSE
  • 5. total = total + 5
  • END IF

The numbered lines are the statements; IF, ELSE and END IF lines are not counted as statements. Each condition tests its own variable, so any combination of outcomes is possible.

Two tests run it, one with a = 4, b = 7 and one with a = 12, b = 9. What statement coverage do they achieve?

What the syllabus asks for

Statement coverage is the share of executable statements that the tests run. Branch coverage is the share of branches they exercise, and branches include the true and false outcomes of every decision, so an IF without an ELSE still has a false branch that some test must take. Branch coverage subsumes statement coverage: 100% branch coverage also achieves 100% statement coverage, but not the other way round.

The questions here use short programs with IF and IF-ELSE and no loops, and the minimum number of tests is found by trying every set of paths. The cyclomatic complexity appears among the wrong answers on purpose: it is an upper bound for branch coverage, not the minimum.

Worked examples

Example 1

A function contains this code:

  • 1. count = count + 1
  • IF a > 6 THEN
  • 2. discount = 10
  • END IF
  • 3. total = total + 5
  • IF b > 2 THEN
  • 4. log "checked"
  • END IF
  • 5. fee = 0

The numbered lines are the statements; IF, ELSE and END IF lines are not counted as statements. Each condition tests its own variable, so any combination of outcomes is possible.

One test runs it with a = 3, b = 9. What statement coverage does it achieve?

  • A. 80%
  • B. 20%
  • C. 86%
  • D. 50%
Show the answer

Answer: 80%. The code has 5 statements. The test runs statements 1, 3, 4, 5: 4 of 5, which is 80%.

  • 20%: That is the share of statements not run.
  • 86%: That counts the IF lines as statements, which the question rules out.
  • 50%: That is the share of IF outcomes taken (each IF has two), not the share of statements run.

Try this one in the quiz: open example 1.

Example 2

A function contains this code:

  • 1. log "checked"
  • IF a > 4 THEN
  • 2. send email
  • ELSE
  • 3. count = count + 1
  • END IF
  • 4. print "approved"

The numbered lines are the statements; IF, ELSE and END IF lines are not counted as statements. Each condition tests its own variable, so any combination of outcomes is possible.

One test runs it with a = 0. What statement coverage does it achieve?

  • A. 50%
  • B. 80%
  • C. 75%
  • D. 25%
Show the answer

Answer: 75%. The code has 4 statements. The test runs statements 1, 3, 4: 3 of 4, which is 75%.

  • 50%: That is the share of IF outcomes taken (each IF has two), not the share of statements run.
  • 80%: That counts the IF lines as statements, which the question rules out.
  • 25%: That is the share of statements not run.

Try this one in the quiz: open example 2.

Example 3

A function contains this code:

  • 1. status = "review"
  • IF a > 3 THEN
  • 2. send email
  • ELSE
  • 3. print "approved"
  • END IF
  • IF b > 8 THEN
  • 4. retry = 3
  • ELSE
  • 5. flag = true
  • END IF
  • IF c > 5 THEN
  • 6. print "approved"
  • END IF
  • 7. fee = 0

The numbered lines are the statements; IF, ELSE and END IF lines are not counted as statements. Each condition tests its own variable, so any combination of outcomes is possible.

One test runs it with a = 10, b = 12, c = 11. What statement coverage does it achieve?

  • A. 29%
  • B. 50%
  • C. 71%
  • D. 80%
Show the answer

Answer: 71%. The code has 7 statements. The test runs statements 1, 2, 4, 6, 7: 5 of 7, which is 71%.

  • 29%: That is the share of statements not run.
  • 50%: That is the share of IF outcomes taken (each IF has two), not the share of statements run.
  • 80%: That counts the IF lines as statements, which the question rules out.

Try this one in the quiz: open example 3.

What this is not

These are not questions from any ISTQB exam, and this site has no connection with ISTQB or any exam provider. Older question banks often use definitions from before version 4.0, so if an answer here disagrees with one you’ve seen elsewhere, check which syllabus it was written for. How the questions are made explains how every answer is checked.

Other techniques