White-Box & Cyclomatic Complexity Questions with Answers

15 practice questions on White-Box & Cyclomatic Complexity (Software Engineering & Testing) with answers and step-by-step solutions. Try 10 samples below, then practise all 15 free in the AptiNest app.

Sample questions

Question 1Easy

A program's control flow graph has 17 edges and 13 nodes. What is its cyclomatic complexity?

  1. 5
  2. 6
  3. 4
  4. 8

Answer: B. 6

Explanation

V(G) = E - N + 2
= 17 - 13 + 2
= 6.

Step-by-step solution

  1. E = 17 edges and N = 13 nodes.
  2. P = 1 for one program.
  3. V(G) = 17 − 13 + 2 × 1 = 6: option B.

Why the other options are wrong

  • A. 5 would need E − N = 3, but 17 − 13 = 4.
  • C. 4 would need E − N = 2, which does not match 17 − 13.
  • D. 8 would need E − N = 6, which does not match 17 − 13.

Tip: For one routine, V(G) = E − N + 2.

Question 2Easy

Basis path testing is a technique from which category?

  1. Acceptance testing
  2. White-box testing
  3. Black-box testing
  4. Usability testing

Answer: B. White-box testing

Explanation

It uses the control flow graph of the code to derive independent paths, so it needs the source code.

Step-by-step solution

  1. Basis path testing uses the control-flow graph.
  2. It needs knowledge of the code structure.
  3. So it is white-box.

Why the other options are wrong

  • A. Acceptance testing checks requirements with users.
  • C. Black-box ignores code structure.
  • D. Usability testing studies user experience.

Question 3Easy

McCabe's cyclomatic complexity of a control flow graph is given by:

  1. V(G) = N - E + 2P
  2. V(G) = E - N + 2P
  3. V(G) = E + N - 2P
  4. V(G) = E x N / P

Answer: B. V(G) = E - N + 2P

Explanation

E is the number of edges, N the number of nodes and P the number of connected components (1 for a single program).

Step-by-step solution

  1. Cyclomatic complexity counts independent paths in a control flow graph.
  2. The formula uses edges (E), nodes (N) and connected components (P).
  3. V(G) = E − N + 2P: option B.

Why the other options are wrong

  • A. N − E + 2P swaps edges and nodes, which gives a wrong and often negative value.
  • C. E + N − 2P does not match the formula.
  • D. E × N ÷ P is not the formula for cyclomatic complexity.

Tip: V(G) = edges − nodes + 2 for one routine: edges minus nodes, plus 2.

Question 4Medium

What is the cyclomatic complexity of this function?

int f(int a, int b) {
    if (a > 0) {
        b++;
    }
    while (b < 10) {
        if (b % 2 == 0)
            b += 3;
        else
            b++;
    }
    return b;
}
  1. 5
  2. 3
  3. 4
  4. 6

Answer: C. 4

Explanation

There are 3 decision points (if, while, inner if). V(G) = decisions + 1 = 4. The else does not add a separate decision.

Step-by-step solution

  1. Decision points: if (a > 0), while (b < 10) and the inner if (b % 2 == 0).
  2. The else does not add a new decision.
  3. V(G) = 3 decisions + 1 = 4: option C.

Why the other options are wrong

  • A. 5 would need four decision points, but there are three.
  • B. 3 misses one decision; the value is decisions + 1.
  • D. 6 would need five decision points, but the code has three.

Tip: For structured code, V(G) = number of decision points (if, while, for, case) + 1.

Question 5Medium

For the code below, a single test with x = 6, y = 1 is run. What branch (decision) coverage does it achieve?

read x, y
if (x > 5)
    z = x + y
if (y > 2)
    z = z * 2
print z
  1. 25%
  2. 100%
  3. 50%
  4. 75%

Answer: C. 50%

Explanation

There are 4 branch outcomes (each if true and false). The test covers 'x > 5' true and 'y > 2' false, so 2 of 4 = 50%.

Step-by-step solution

  1. The code has 2 if statements, so 4 branch outcomes: each true and false.
  2. With x = 6, 'x > 5' is true, so that true branch is covered.
  3. With y = 1, 'y > 2' is false, so that false branch is covered.
  4. 2 of 4 outcomes = 50%: option C.

Why the other options are wrong

  • A. 25% would mean only 1 of 4 outcomes, but two are covered.
  • B. 100% would need the y > 2 true and the x > 5 false outcomes too.
  • D. 75% would need 3 of the 4 outcomes, but only 2 are covered.

Tip: Branch coverage = outcomes covered ÷ total outcomes; each if gives two outcomes.

Question 6Medium

Which statement about coverage criteria is correct?

  1. 100% statement coverage implies 100% branch coverage
  2. Statement and branch coverage are always equal
  3. 100% path coverage is usually cheaper than branch coverage
  4. 100% branch coverage implies 100% statement coverage, but not the other way round

Answer: D. 100% branch coverage implies 100% statement coverage, but not the other way round

Explanation

Covering every branch outcome executes every statement. An if with no else can be fully statement-covered by one test that never takes the false branch.

Step-by-step solution

  1. Covering every branch outcome also runs every statement.
  2. An if without else can be statement-covered without taking the false branch.
  3. So 100% branch coverage implies 100% statement coverage, but not the other way round: option D.

Why the other options are wrong

  • A. 100% statement coverage can miss a branch outcome, such as an if with no else that is never false.
  • B. Statement and branch coverage are not always equal.
  • C. Path coverage is usually more costly than branch coverage, not cheaper.

Tip: Coverage strength order: statement, then branch, then path; each stronger level implies the weaker ones.

Question 7Medium

The cyclomatic complexity of a function gives:

  1. The number of lines of code
  2. The exact number of defects in the function
  3. The number of linearly independent paths, an upper bound on tests needed for basis path coverage
  4. The total number of possible execution paths through the function, including every loop iteration count

Answer: C. The number of linearly independent paths, an upper bound on tests needed for basis path coverage

Explanation

V(G) counts independent paths; basis path testing needs at most V(G) test cases to execute each of them.

Step-by-step solution

  1. Cyclomatic complexity V(G) counts the linearly independent paths through a function.
  2. Basis path testing needs at most V(G) test cases to run each of these paths.
  3. So it gives an upper bound on tests for basis path coverage: option C.

Why the other options are wrong

  • A. Lines of code is a size measure, not complexity.
  • B. V(G) does not count defects.
  • D. Total execution paths including all loop counts is not what V(G) measures; it counts independent paths.

Tip: V(G) = independent paths; it is the number of basis tests, not the number of all paths.

Question 8Medium

A control flow graph has edges: 1-2, 2-3, 2-4, 3-5, 4-5, 5-6, 6-2, 6-7, 7-8, 5-8 (8 nodes, one connected component). What is its cyclomatic complexity?

  1. 6
  2. 2
  3. 5
  4. 4

Answer: D. 4

Explanation

E = 10, N = 8, P = 1, so
V(G) = 10 - 8 + 2 x 1 = 4.

Step-by-step solution

  1. Count the edges: 1-2, 2-3, 2-4, 3-5, 4-5, 5-6, 6-2, 6-7, 7-8, 5-8 gives E = 10.
  2. Nodes N = 8, and P = 1 connected component.
  3. V(G) = 10 − 8 + 2 × 1 = 4: option D.

Why the other options are wrong

  • A. 6 would need 12 edges with 8 nodes, but there are 10 edges.
  • B. 2 is far below the value from E − N + 2P.
  • C. 5 is one more than the correct value 10 − 8 + 2 = 4.

Tip: Count the edges and nodes carefully; V(G) = E − N + 2 for one connected graph.

Question 9Hard

A program consists of a main routine and two subroutines analysed together. Their combined control flow graphs have 20 edges, 18 nodes and 3 connected components. What is the cyclomatic complexity?

  1. 8
  2. 5
  3. 2
  4. 4

Answer: A. 8

Explanation

V(G) = E - N + 2P
= 20 - 18 + 2 x 3
= 8.
Each component (each routine) contributes its own +2.

Step-by-step solution

  1. E = 20 edges, N = 18 nodes and P = 3 components.
  2. Each routine adds its own 2, so 2P = 6.
  3. V(G) = 20 − 18 + 6 = 8: option A.

Why the other options are wrong

  • B. 5 would be 2 + 3, which does not use the 2P term correctly.
  • C. 2 ignores the edges and nodes entirely.
  • D. 4 would be E − N + 2 with P = 1, but there are 3 components.

Tip: With P components, V(G) = E − N + 2P.

Question 10Hard

Consider:

if (a > 0)
    if (b > 0)
        S1
    else
        S2
else
    S3

What is the minimum number of tests needed for 100% statement coverage?

  1. 3
  2. 4
  3. 1
  4. 2

Answer: A. 3

Explanation

S1, S2 and S3 lie on mutually exclusive paths, so each needs its own test: (a > 0, b > 0), (a > 0, b <= 0) and (a <= 0).

Step-by-step solution

  1. Three statements S1, S2 and S3 each sit on a different path.
  2. These paths are mutually exclusive, so one test cannot run two of them.
  3. Each needs its own test: a > 0 with b > 0 (S1), a > 0 with b ≤ 0 (S2), and a ≤ 0 (S3).
  4. The minimum is 3: option A.

Why the other options are wrong

  • B. 4 is more than needed; the three statements are covered by three tests.
  • C. 1 test can only run one of the three mutually exclusive statements.
  • D. 2 tests cannot cover all three exclusive statements.

Tip: For mutually exclusive statements, the count is one test per statement.

Practise all 15 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Software Engineering & Testing topics · All topics