NivaarExam PrepOfficial exam papers ↗

19-Soft-A6 Software Quality Assurance · May 2015

Question 8 of 8

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

04-Soft-A6, Software Quality Assurance — National Exams, May 2015 (3 hours, open book, 8 questions of equal value; the first FIVE as they appear in the answer book are marked — all eight are solved here as a study resource).

Reference texts: Pressman, Software Engineering: A Practitioner's Approach, 9th ed. (SQA planning, review, testing strategies/techniques, cyclomatic complexity, basis path testing); Sommerville, Software Engineering, 10th ed. (software process, configuration management); ISO/IEC 25010 SQuaRE (software quality characteristics); ISO/IEC 12207 (life-cycle/configuration-management processes).

Question 8 (10 marks)

Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.

Given. The unit under test is the nested-loop body of GeneratePyramidExample.main (Appendix B, lines 21–31): an outer loop for(i=0; i<=as; i++) containing an inner loop for(j=1; j≤i; j++) that prints a running total y (initialised once, outside both loops, and never reset).

Find. The cyclomatic complexity V(G) of the unit, and a minimal set of V(G) basis-path test cases (each input pair (as, x) plus its exact expected console output) that together execute every independent path through the control-flow graph.

1 2 3 4 5 6 y = 0 true (i≤as) false (i>as) true (j≤i) false (j>i) print/y+=x (back to 3) println("") (back to 2) V(G) = E - N + 2 = 7 - 6 + 2 = 3 = 1 + (2 predicate nodes: 2, 3 each contribute 1) = 3
Control-flow graph of the pyramid unit. Node 1: y=0. Node 2: outer for-test. Node 3: inner for-test. Node 4: print(y);y+=x. Node 5: println(""). Node 6: exit. Nodes 2 and 3 are the two predicate (decision) nodes.

Approach. Draw the flow graph of the nested-loop unit, compute V(G) by both the edge–node formula and the predicate-node formula (they must agree), then derive V(G) independent basis paths by taking a baseline path and flipping one decision outcome at a time, and choose concrete (as, x) inputs that force each path.

  1. Build the flow graph and count nodes/edges. Six nodes (1: init; 2: outer test; 3: inner test; 4: inner body; 5: end-of-row println; 6: exit) and seven edges (1→2; 2→3 true; 2→6 false; 3→4 true; 3→5 false; 4→3 back-edge; 5→2 back-edge).
  2. Compute V(G) two independent ways. $$V(G) = E - N + 2 = 7 - 6 + 2 = \boxed{3}$$ $$V(G) = 1 + \sum_{\text{predicates}}(\text{out-degree}-1) = 1 + (2-1) + (2-1) = \boxed{3}$$ Both formulas agree at V(G) = 3, so exactly 3 independent basis-path test cases are required.
  3. Path 1: outer test fails immediately (nodes 1-2-6). The outer loop body never executes at all — this requires as < 0, since i starts at 0 and i ≤ as must be false on the very first check.
    Input: as = -1, x = 5 → Expected console output: (nothing printed — zero rows)
  4. Path 2: outer loop runs, inner test fails on its only visit (nodes 1-2-3-5-2-6). This is forced by as = 0: the single outer iteration has i = 0, so the inner test j ≤ i (i.e. 1 ≤ 0) is false immediately, the inner body never runs, and only the row-ending println("") executes before the outer loop exits.
    Input: as = 0, x = 5 → Expected console output: one blank line
  5. Path 3: inner loop body executes at least once (nodes 1-2-3-4-3-5-2-6). This requires an outer iteration with i ≥ 1. Substituting as = 2, x = 1 and tracing by hand: i=0 → inner skipped, blank row; i=1 → inner runs once, prints y=0 then y becomes 1, row = "0\t"; i=2 → inner runs twice, prints y=1 (then y→2) and y=2 (then y→3), row = "1\t2\t".
    Input: as = 2, x = 1 → Expected console output (3 lines):
      (blank)
      0    (tab)
      1    2    (tabs)
    Note that y is declared once before the outer loop and is never reset per row — it is a running total across the ENTIRE pyramid, not a fresh count restarting at 0 on each row, which is why row i=2 continues 1, 2 rather than restarting at 0, 1.
Basis-path test suite for GeneratePyramidExample
PathNodes traversedInput (as, x)Expected output
11-2-6(−1, 5)No rows printed
21-2-3-5-2-6(0, 5)One blank row
31-2-3-4-3-5-2-6(2, 1)Rows: "", "0\t", "1\t2\t"
Check: assumes a normal (non-exceptional) input for as and x — a full test plan would add an error-handling path for non-numeric console input (Integer.parseInt throwing NumberFormatException), which is outside the 3 basis paths of the arithmetic control flow itself and would be covered separately by black-box/robustness testing (Q7's technique), not by this white-box basis-path count.
Back to the paper →