in DS edited by
14,305 views
52 votes
52 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
in DS edited by
by
14.3k views

1 comment

1. the problem when  old char is replaced and then while checking we again check

it for like 2 time or 1 time more and so in way we are considering it as old

char  because code is flawed in that way.

2.So to catch this what should happen is you replace old char with some new   

char and then that should be again replaced with new char

3.And now it shouldn't repeat again otherwise it may nullify all the changes

which we detected till now, so be careful and check for it though that is   

not happening in given question
0
0

9 Answers

51 votes
51 votes
Best answer
The test cases $3$ and $4$ are the only cases that capture the flaw. The code does not 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).$

Correct Answer: B.
edited by

4 Comments

Sir, please provide example for this problem to understand in detail
1
1
Please provide examples...merely explanation does not suffice
1
1
sir, will u plz epalain me hw will we know that two times replacement of a character is flaw? bcuz in question it is not mentioned.
0
0
44 votes
44 votes

$\require{cancel}A\begin{array}{|c|c|c|c|c|}\hline d\cancel{a}&a\cancel{b}&b\cancel{c}&d&e\\ \hline\end{array}$

$\require{cancel}\begin{array}{|c|c|c|c|}\hline \textbf{oldc}&b&c&a\\\hline \textbf{newc}& c&d&a\\ \hline\end{array}$

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.

edited by

4 Comments

@srestha What is mean by flaws here i did not get question

0
0
flaw means, which is unwanted according to the code
1
1
thank you ma'am
0
0
24 votes
24 votes
For array $A$ check its each character with every character in $oldc$ sequentially.

$1. \begin{array}{|c|c|c|}\hline \color{green}a&\color{green}b&\color{green}c \\ \hline d&a&b \\ \hline\end{array}\checkmark \qquad \qquad 2. \begin{array}{|c|c|c|}\hline \color{green}c&\color{green}d&\color{green}e \\ \hline b&c&d \\ \hline\end{array}\checkmark$

$3. \begin{array}{|c|c|c|}\hline \color{green}b&\boxed{\color{red}c}&\color{green}a \\ \hline \boxed{\color{red}c}&d&a \\ \hline\end{array}\times \qquad 4. \begin{array}{|c|c|c|}\hline \color{green}a&\boxed{\color{red}b}&\color{green}c \\ \hline \boxed{\color{red}b}&a&c \\ \hline\end{array}\times$

Correct Answer: B.
edited by
1 vote
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.
Answer:

Related questions