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?

Choose one option
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 A="abcde"A = \text{"abcde"}:

  • Test case 1: oldc = "abc", newc = "dab"
    'a' →\rightarrow 'd', 'b' →\rightarrow 'a', 'c' →\rightarrow 'b'. Result is "dabde", which matches the expected replacement. Flaw not exposed.

  • Test case 2: oldc = "cde", newc = "bcd"
    'c' →\rightarrow 'b', 'd' →\rightarrow 'c', 'e' →\rightarrow 'd'. Result is "abbcd", which matches expected. Flaw not exposed.

  • Test case 3: oldc = "bca", newc = "cda"
    Expected output: 'a' →\rightarrow 'a', 'b' →\rightarrow 'c', 'c' →\rightarrow 'd', 'd' →\rightarrow 'd', 'e' →\rightarrow 'e'   ⟹  \implies "acdde".
    Actual execution on A[1]=’b’A[1] = \text{'b'}:

    • At j=0j = 0: A[1]==’b’A[1] == \text{'b'}, so A[1]A[1] is updated to 'c'.
    • At j=1j = 1: now A[1]==’c’A[1] == \text{'c'}, which matches oldc[1] == 'c', so A[1]A[1] 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' →\rightarrow 'b', 'b' →\rightarrow 'a', 'c' →\rightarrow 'c'   ⟹  \implies "bacde".
    Actual execution on A[0]=’a’A[0] = \text{'a'}:

    • At j=0j = 0: A[0]==’a’A[0] == \text{'a'}, so A[0]A[0] is updated to 'b'.
    • At j=1j = 1: now A[0]==’b’A[0] == \text{'b'}, which matches oldc[1] == 'b', so A[0]A[0] is updated to 'a'!
      Actual output leaves A[0]A[0] 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).

Final answer

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.