Consider the following method: int f(int m, int n, boolean x, boolean y) { int…

2018

Consider the following method:

int f(int m, int n, boolean x, boolean y)

{

int res=0;

if (m<0) {res=n-m;}

else if (x || y)

{

res=-1;

if(n==m){res=1;}

}

else {res=n;}

return res;

} /*end of f */

If 𝑃 is the minimum number of tests to achieve full statement coverage for 𝑓(), and 𝑄 is the minimum number of tests to achieve full branch coverage for 𝑓(), then (𝑃,𝑄) = 

  1. A.

    (3,4)

  2. B.

    (4,3)

  3. C.

    (2,3)

  4. D.

    (3,2)

Attempted by 217 students.

Show answer & explanation

Correct answer: A

Answer: (P, Q) = (3, 4).

Reasoning:

  • Key executable statements to cover: initialization (res = 0), assignment when m < 0 (res = n - m), assignment when x || y (res = -1), nested assignment when n == m (res = 1), and final else assignment (res = n).

  • Statement coverage (P): We need tests that execute each of the statements above. Minimal set of 3 tests:

    • Test 1: m = -1, n = 5, x = false, y = false — executes the m < 0 branch and res = n - m.

    • Test 2: m = 2, n = 2, x = true, y = false — executes the x || y branch and the nested n == m true case (res = -1 then res = 1).

    • Test 3: m = 2, n = 3, x = false, y = false — executes the final else (res = n).

  • Branch coverage (Q): The conditionals are if (m < 0), else if (x || y), and inside that if (n == m). Each conditional has two outcomes (true/false). To exercise both outcomes for the outer if, the else-if, and the nested if, we need at least 4 tests:

    • Test A: m = -1, n = 5, x = false, y = false — covers m < 0 = true.

    • Test B: m = 2, n = 3, x = false, y = false — covers m < 0 = false and x || y = false.

    • Test C: m = 2, n = 2, x = true, y = false — covers m < 0 = false, x || y = true, and n == m = true.

    • Test D: m = 2, n = 3, x = true, y = false — covers m < 0 = false, x || y = true, and n == m = false.

Therefore the minimum number of tests for full statement coverage is 3, and for full branch coverage is 4.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor