GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Detect cascading character replacement without a break

2 questions solved the same way, newest first. Back to Programming in C

How these are solved

Both inspect the same nested find_and_replace loop and the same four oldc/newc mappings. A newly replaced character can match a later oldc entry because the inner loop continues.

2 marks · MCQ
Report issue

Which replacement maps expose cascading substitutions

Arrays & Strings

Question 50, GATE 2013: Code loops i=0…4 then j=0…2, and if A[i]==oldc[j] immediately sets A[i]=newc[j], without breaking. Maps are 1 abc→dab;2 cde→bcd;3 bca→cda;4 abc→bac. Testing every 5-character input from{a, b, c, d, e}, how many maps expose its replacement flaw? A1, B2, C3, D4.
Question 50, GATE 2013: Code loops i=0…4 then j=0…2, and if A[i]==oldc[j] immediately sets A[i]=newc[j], without breaking. Maps are 1 abc→dab;2 cde→bcd;3 bca→cda;4 abc→bac. Testing every 5-character input from{a, b, c, d, e}, how many maps expose its replacement flaw? A1, B2, C3, D4.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

The defect is that the inner loop continues after a match, so a replacement character can match a later oldc entry and be replaced again. In case 1 (abc→dab), a becomes d, b becomes a after the a-entry has already been passed, and c becomes b after the b-entry has been passed. No input character cascades. In case 2 (cde→bcd), each replacement is absent from the remaining suffix of oldc, so again no character cascades. In case 3 (bca→cda), an original b first becomes c and then becomes d; the intended one-step result is c. In case 4 (abc→bac), an original a first becomes b and then becomes a; the intended result is b. Exhaustive length-five input testing includes strings containing b and a, respectively, so exactly cases 3 and 4 can reveal the flaw. That is two cases, option B. The five-character array need not be NUL-terminated because the code accesses exactly its five given elements.

The intended specification performs one replacement based on the original character, whereas the code repeatedly tests the updated character. For each map, inspect whether a replacement equals an oldc entry at a later j. Only later entries matter: a match with an earlier entry is never revisited. Map3 has b→c with c later, and map 4 has a→b with b later. Maps 1and2 only map backward or outside the remaining entries, so no repeated substitution can occur. Exhaustive input testing guarantees witnesses containing the affected characters.

Original paper, page 11 · Answer key

2 marks · MCQ
Report issue

Replacement-map failures on abcde

Arrays & Strings

Question 51, GATE 2013: Use the same nested replacement loops and four maps 1 abc→dab;2 cde→bcd;3 bca→cda;4 abc→bac. For input abcde specifically, which expose the flaw? A none; B2 only; C3and4; D4 only.
Question 51, GATE 2013: Use the same nested replacement loops and four maps 1 abc→dab;2 cde→bcd;3 bca→cda;4 abc→bac. For input abcde specifically, which expose the flaw? A none; B2 only; C3and4; D4 only.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Apply each map once to each original character to obtain the intended result, then compare with the procedure’s sequential replacements. For input abcde, case 1 has intended and actual output dabde. Case 2 has intended and actual output abbcd. Case 3 should produce acdde, but its b is changed twice, b→c→d, producing addde. Case 4 should produce bacde, but its initial a is changed twice, a→b→a, producing aacde. Therefore cases 3 and 4 expose the defect, option C.

The fixed input contains every candidate letter once, including both b and a that trigger the two flaws. In map 3 the intended b→c is followed by an unintended c→d, changing the second character from expected c to d. In map 4 the intended a→b is followed by unintended b→a, undoing that substitution. Comparing complete strings confirms the discrepancy rather than relying merely on the presence of an intermediate match. Cases 1and2remain unaffected.

Original paper, page 11 · Answer key