The GATE Grind

GATE 2013 CS (CS1) – Question 61

Programming and Data Structures · Programming in C · 2 marks · Multiple choice

A has length five; oldc,newc each have length three. The intended operation replaces each original A character at most once using the corresponding mapping.

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];
}

Tests: (1) oldc="abc",newc="dab"; (2) "cde","bcd"; (3) "bca","cda"; (4) "abc","bac". If A initially holds "abcde", which tests expose the flaw?

  1. None
  2. 2 only
  3. 3 and 4 only
  4. 4 only

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (C) 3 and 4 only

Explanation

The input contains both b and a. Test three incorrectly cascades b→c→d, while test four cascades a→b→a. Tests one and two avoid forward rematches and produce the intended one-step substitutions. Thus exactly tests three and four expose the defect, C.

More Programming in C questions

All Programming in C questions →