The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+22 votes

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
asked in DS by Veteran (407k points)
edited by | 2.6k views

7 Answers

+20 votes
Best answer

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

So, answer is B.

answered by Veteran (111k points)
edited by


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

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
U typed B instead of C in the end.


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





+23 votes

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

answered by Boss (13.2k points)
Plz provide some examples to explain in some detail..
please consider my answer and tell ur take on that?
Sir, please provide example for this problem to understand in detail
+8 votes

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

answered by Boss (30.6k points)
edited by
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.
answered by Boss (15.9k points)
0 votes
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.

Answer (B)
answered by Active (2k points)
–3 votes

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!


answered by Active (3.1k points)
that's not an issue in given code. For each character it is checking all 3 characters in oldc.
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
@Arjun, Can you please explain in Q 50, why won't test case 2 (cde-> bcd) work on input {ccccc} (repetitions are allowed)
–3 votes

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]  

answered by Active (2.4k points)
reshown by

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,535 questions
54,122 answers
71,040 users