Sample questions
Question 1Easy
A program's control flow graph has 17 edges and 13 nodes. What is its cyclomatic complexity?
- 5
- 6
- 4
- 8
Answer: B. 6
Explanation
V(G) = E - N + 2
= 17 - 13 + 2
= 6.
Step-by-step solution
- E = 17 edges and N = 13 nodes.
- P = 1 for one program.
- 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?
- Acceptance testing
- White-box testing
- Black-box testing
- 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
- Basis path testing uses the control-flow graph.
- It needs knowledge of the code structure.
- 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:
- V(G) = N - E + 2P
- V(G) = E - N + 2P
- V(G) = E + N - 2P
- 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
- Cyclomatic complexity counts independent paths in a control flow graph.
- The formula uses edges (E), nodes (N) and connected components (P).
- 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;
}
- 5
- 3
- 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
- Decision points: if (a > 0), while (b < 10) and the inner if (b % 2 == 0).
- The else does not add a new decision.
- 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
- 25%
- 100%
- 50%
- 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
- The code has 2 if statements, so 4 branch outcomes: each true and false.
- With x = 6, 'x > 5' is true, so that true branch is covered.
- With y = 1, 'y > 2' is false, so that false branch is covered.
- 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?
- 100% statement coverage implies 100% branch coverage
- Statement and branch coverage are always equal
- 100% path coverage is usually cheaper than branch coverage
- 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
- Covering every branch outcome also runs every statement.
- An if without else can be statement-covered without taking the false branch.
- 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:
- The number of lines of code
- The exact number of defects in the function
- The number of linearly independent paths, an upper bound on tests needed for basis path coverage
- 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
- Cyclomatic complexity V(G) counts the linearly independent paths through a function.
- Basis path testing needs at most V(G) test cases to run each of these paths.
- 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?
- 6
- 2
- 5
- 4
Answer: D. 4
Explanation
E = 10, N = 8, P = 1, so
V(G) = 10 - 8 + 2 x 1 = 4.
Step-by-step solution
- 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.
- Nodes N = 8, and P = 1 connected component.
- 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?
- 8
- 5
- 2
- 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
- E = 20 edges, N = 18 nodes and P = 3 components.
- Each routine adds its own 2, so 2P = 6.
- 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?
- 3
- 4
- 1
- 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
- Three statements S1, S2 and S3 each sit on a different path.
- These paths are mutually exclusive, so one test cannot run two of them.
- Each needs its own test: a > 0 with b > 0 (S1), a > 0 with b ≤ 0 (S2), and a ≤ 0 (S3).
- 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 →