Common data questions
Common Data for Questions 50 and 51:
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"
Shared with question 51.
Question
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?
Official answer and solution
- Official answer
- B
- From
- Official GATE 2013 answer key
Solution
Examine the nested loop logic in find_and_replace to determine when the lack of a break statement leads to unintended cascaded replacements.
The procedure is:
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 intended specification is to replace each character in matching oldc[j] with newc[j] exactly once.
However, the inner loop over continues even after a match occurs and is modified. If the newly written character newc[j] matches a subsequent oldc[k] with , the character will be erroneously replaced a second time during the same pass!
Therefore, a test case can expose the flaw if and only if there exist indices such that:
Let us check each of the four test cases:
-
Test case 1:
oldc = "abc", newc = "dab"- :
oldc[0] = 'a',newc[0] = 'd'. 'd' is not inoldc[1..2]('b', 'c'). - :
oldc[1] = 'b',newc[1] = 'a'. 'a' is not inoldc[2]('c'). - No cascading match exists for any input string. Flaw is NOT captured.
- :
-
Test case 2:
oldc = "cde", newc = "bcd"- :
oldc[0] = 'c',newc[0] = 'b'. 'b' is not inoldc[1..2]('d', 'e'). - :
oldc[1] = 'd',newc[1] = 'c'. 'c' is not inoldc[2]('e'). - No cascading match exists. Flaw is NOT captured.
- :
-
Test case 3:
oldc = "bca", newc = "cda"- :
oldc[0] = 'b',newc[0] = 'c'. - Notice that for ,
oldc[1] = 'c'! Here andnewc[0] == oldc[1]. - If , at it is replaced by 'c', and then at it matches
oldc[1]and is replaced again by 'd'. The flaw is exposed!
- :
-
Test case 4:
oldc = "abc", newc = "bac"- :
oldc[0] = 'a',newc[0] = 'b'. - Notice that for ,
oldc[1] = 'b'! Here andnewc[0] == oldc[1]. - If , at it becomes 'b', and then at it matches
oldc[1]and becomes 'a' again. The flaw is exposed!
- :
Thus, exactly two test cases (Test cases 3 and 4) will be able to capture the flaw. Option (B) is correct.
B
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2013 Computer Science and Information Technology (CS)
- Question
- 50 of 65, printed as CS Q.50
- Section
- Computer Science and Information Technology
This question is from a part of the syllabus that is no longer in GATE CS. It is kept because it was in the paper.
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.