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 50.
Question
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?
Official answer and solution
- Official answer
- C
- From
- Official GATE 2013 answer key
Solution
Trace the execution of find_and_replace on the specific input string "abcde" for each test case.
For the input string :
-
Test case 1:
oldc = "abc", newc = "dab"
'a' 'd', 'b' 'a', 'c' 'b'. Result is "dabde", which matches the expected replacement. Flaw not exposed. -
Test case 2:
oldc = "cde", newc = "bcd"
'c' 'b', 'd' 'c', 'e' 'd'. Result is "abbcd", which matches expected. Flaw not exposed. -
Test case 3:
oldc = "bca", newc = "cda"
Expected output: 'a' 'a', 'b' 'c', 'c' 'd', 'd' 'd', 'e' 'e' "acdde".
Actual execution on :- At : , so is updated to 'c'.
- At : now , which matches
oldc[1] == 'c', so is updated to 'd'!
Actual result produces 'd' instead of 'c' at position 1. The flaw is exposed.
-
Test case 4:
oldc = "abc", newc = "bac"
Expected output: 'a' 'b', 'b' 'a', 'c' 'c' "bacde".
Actual execution on :- At : , so is updated to 'b'.
- At : now , which matches
oldc[1] == 'b', so is updated to 'a'!
Actual output leaves as 'a' instead of 'b'. The flaw is exposed.
Therefore, test cases 3 and 4 only will be successful in exposing the flaw, corresponding to option (C).
C
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
- 51 of 65, printed as CS Q.51
- 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.