2.4k views

The procedure given below is required to find and replace certain characters inside an input character string supplied in array $A$. The characters to be replaced are supplied in array $oldc$, while their respective replacement characters are supplied in array $newc$. Array $A$ has a fixed length of five characters, while arrays $oldc$ and $newc$ contain three characters each. However, the procedure is flawed.

void find_and_replace (char *A, char *oldc, char *newc) {
for (int i=0; i<5; i++)
for (int j=0; j<3; j++)
if (A[i] == oldc[j])
A[i] = newc[j];
} 

The procedure is tested with the following four test cases.

1. $oldc = “abc”, newc = “dab”$
2. $oldc = “cde”, newc = “bcd”$
3. $oldc = “bca”, newc = “cda”$
4. $oldc = “abc”, newc = “bac”$

The tester now tests the program on all input strings of length five consisting of characters ‘$a$’, ‘$b$’, ‘$c$’, ‘$d$’ and ‘$e$’ with duplicates allowed. If the tester carries out this testing with the four test cases given above, how many test cases will be able to capture the flaw?

1. Only one
2. Only two
3. Only three
4. All four
edited | 2.4k views

Here, when the element of array $A$ and $oldc$ match , we replace that array element of $A$ with  array element of $newc$ . For every element of $A$ array update occurs maximum one time.

Similarly for $(2)$ array element of $A$ has updated with array element of $newc$ less than or equal to one time,

Now, for (3) when $i=0$ , value of $A$ match with $oldc[2]$ i.e.'$a$' , and replace with $newc[2]$ i.e. also '$a$'. So, no changes

When $i=1$ value of array $A[1]=$'$b$'

Match with $oldc[0]=$'$b$' and replace with $newc[0]=$'$c$'.

Now, $A[1]=$'$c$' which equal with next element of $oldc[1]=$'$c$'.

So, replace again with $newc[1]=$'$d$'.

Now, we can say here in array $A[1]$ value replace with $newc[0]$ value , and that $newc[0]$ value replace with next $newc[1]$ value.

Similarly for (4) here $2$ times replacement for $A[0]$ with element $newc[0]$ and $newc[1]$

Updating of $newc$ value with another $newc$ value is calling flaw here

edited
0

"Updating of newc value with another newc value is calling flaw here " explain this with ur example pls m not getting ur statement

0
Actually definition of flaw not given in detail. After implementation of code we got what they actually mean by flaw.

just execute the program u will get what flaw code is giving
0
U typed B instead of C in the end.
0

Could you please explain why option A is not flaw here

a b c

d a  b .Now a can again be replaced by d and b replaced  by a . Why is it not flaw here.

d d a

+1

@Sutanay3..

The test cases 3 and 4 are the only cases that capture the flaw. The code doesn't work properly when an old character is replaced by a new character and the new character is again replaced by another new character. This doesn't happen in test cases (1) and (2), it happens only in cases (3) and (4).

50. B

51. C

0
Plz provide some examples to explain in some detail..
0
0
Sir, please provide example for this problem to understand in detail

Q.50 = option B
Q.51 = option C

edited
+1
it is B, C rt?
+1 vote
In this question the main key is to find the flaw. The code seems easy so we can see what may be the flaw. After seeing we can guess that there can be a problem if one character is replaced by some character and which is again replaced by the character,

So see the c option. bca and cda. we replace b with c. but now the next check will make c to d. That's why it's wrong.
The option D is also doing the same thing but it will not point out the flaw because we are converting first a to b and then b to a. So it's a kind of reversible process and will not be reflected. This assumption seems wrong but as question is not well formed and option c and D is not there we will go with the best possible answer of C.
The flaw here is in say oldc= xyz and newc = yvw here if in, A contains x at 0th position it will be replaced by y(for x=0,j=0). However when j=1, again that y will be replaced by v (for x=0,j=1) which is wrong.

By now we get the idea that  if 'a' is replaced by say 'x' then x should appear in right of a in oldc.

Test case1  and case2: the above mentioned condition doesn't occur here. Therefore flae will not be detected.

In test3, 'b' is replaced by c and c appears in right of b in oldc. Therefore flaw will be detected.

In test 4, 'a' is replaced by 'b' and b appears rigtr of a in oldc. Therefore flaw will be detected.

51 answer should be D option that is 4 only
test case 3 can't detect its flaw because "bca"(i.e. 3) is not there in the INPUT!

:(

0
that's not an issue in given code. For each character it is checking all 3 characters in oldc.
0
Question says:
"If array A is made to hold the string “abcde”, which of the above four test cases will be successful in exposing the flaw in this procedure?"

if it holds abcde. how will bca catch the input?

as there would not be any change, and the output would be as expected.
abcde( replace bca with something) =abcde
:D
0
@Arjun, Can you please explain in Q 50, why won't test case 2 (cde-> bcd) work on input {ccccc} (repetitions are allowed)

Here the flaw is not using 'break' statement after replacing the character at array A.

if (A[i] == oldc[j])
A[i] = newc[j];break;

If we don't use break then ,the updated character will be again replaced if it is present anywhere at oldc[j+1] ,oldc[j+2]......oldc[2]

reshown by

1
2