Consider the method mcq (); int mcq (boolean a, boolean b, boolean c, boolean…

2017

Consider the method mcq ();

int mcq (boolean a, boolean b, boolean c, boolean d)

{

int ans=1;

if (a) {ans=2;}

else if (b) {ans=3;}

else if (c) {

                if (d) {ans=4;}

                }

return ans;

}

If

𝑀1= Number of tests to exhaustively test mcq();

𝑀2= Minimum number of tests to achieve full statement coverage for mcq (); and

𝑀3= Minimum number of tests to achieve full branch coverage for mcq ( );

then (𝑀1,𝑀2,𝑀3) = ______________ .

  1. A.

    (16,3,5)

  2. B.

    (8,5,3)

  3. C.

    (8,3,5)

  4. D.

    (16,4,4)

Attempted by 32 students.

Show answer & explanation

Correct answer: A

Answer: (16, 3, 5).

Key points: how the counts are derived

  • M1 (exhaustive tests): 2^4 = 16 because there are four independent boolean parameters.

  • M2 (statement coverage): 3 tests suffice to execute every statement at least once:

  1. Test 1: a = true, b = false, c = false, d = false -> executes ans = 2

  2. Test 2: a = false, b = true, c = false, d = false -> executes ans = 3

  3. Test 3: a = false, b = false, c = true, d = true -> executes ans = 4

These three tests cover the initial assignment and each assignment inside the conditional branches, so every statement is executed at least once.

M3 (branch coverage): 5 tests are needed to exercise both true and false outcomes of every condition in their evaluation contexts.

  • Rationale: a must be observed true and false; b must be observed true and false when a is false; c must be observed true and false when a and b are false; d must be observed true and false when a and b are false and c is true.

  1. Test A: a = true, b = false, c = false, d = false (covers a = true outcome)

  2. Test B: a = false, b = true, c = false, d = false (covers b = true outcome; b = false outcome will be in other tests)

  3. Test C: a = false, b = false, c = false, d = false (covers c = false outcome when a and b are false)

  4. Test D: a = false, b = false, c = true, d = true (covers c = true and d = true)

  5. Test E: a = false, b = false, c = true, d = false (covers c = true and d = false)

These five tests exercise both outcomes of each conditional in the contexts where they are evaluated, so they achieve full branch coverage.

Therefore the minimal counts are M1 = 16, M2 = 3, M3 = 5.

Explore the full course: Mppsc Assistant Professor