The procedure given below is required to find and replace certain characters…

20132013

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:

  • oldc = "abc", newc = "dab"

  • oldc = "cde", newc = "bcd"

  • oldc = "bca", newc = "cda"

  • 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?

Answer: B. Only twoConceptThe inner loop scans oldc in increasing index order and overwrites A[i] in place. After a replacement, later comparisons in the same inner loop use the…

  1. A.

    Only one

  2. B.

    Only two

  3. C.

    Only three

  4. D.

    All four

Attempted by 141 students.

Show answer & explanation

Correct answer: B

Concept

The inner loop scans oldc in increasing index order and overwrites A[i] in place. After a replacement, later comparisons in the same inner loop use the newly written character, so one input character may be replaced several times in one pass. A later re-match creates a possible cascade, but a test pair exposes the flaw only when the complete trace ends at a character different from the intended one-step replacement.

To test a pair exactly, start with each possible input character, run all comparisons in order for j = 0, 1, 2, and compare the final character with the intended mapping. The pair exposes the flaw if at least one character finishes differently. Because the tester exhausts every length-five string over the stated alphabet, any such character occurs in some tested input.

Trace each pair to its final value

oldc

newc

Decisive trace

Exposes flaw

abc

dab

a→d; b→a; c→b — all intended results

No

cde

bcd

c→b; d→c; e→d — all intended results

No

bca

cda

b→c→d instead of intended b→c

Yes

abc

bac

a→b→a instead of intended a→b

Yes

Trace the two whose final value changes

  1. bca / cda: take input character 'b'. At j=0, 'b' matches oldc[0] and becomes 'c'. At j=1, that 'c' matches oldc[1] and becomes 'd'. Intended b->c comes out as b->d — flaw observed.

  2. abc / bac: take input character 'a'. At j=0, 'a' becomes 'b'. At j=1, that 'b' matches oldc[1] and becomes 'a'. Intended a->b comes out as a->a (no net change) — flaw observed.

Cross-check

For abc/dab and cde/bcd, no replacement character reappears at a strictly later oldc index, so no input string can trigger a second overwrite; their output always equals the intended one-shot mapping. Hence exactly two of the four test pairs can capture the flaw.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…