edited by
14,210 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
edited by

9 Answers

Best answer
51 votes
51 votes
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
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
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 votes
1 votes
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

27 votes
27 votes
2 answers
2
41 votes
41 votes
4 answers
4
Arjun asked Sep 23, 2014
12,118 views
Which one of the following is the tightest upper bound that represents the time complexity of inserting an object into a binary search tree of $n$ nodes?$O(1)$$O(\log n)$...