GateAI
Programming in C

GATE CSE 2013 · Q51

MCQ2 marksEasy

Time

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

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) 3 and 4 only

Programming in C

Since array A contains the string "abcde", it contains all distinct characters 'a', 'b', 'c', 'd', and 'e':

  • Test case (1): Replaces 'a' \to 'd', 'b' \to 'a', 'c' \to 'b'. No character is replaced more than once. The final string is "dabde", which matches the expected output.

  • Test case (2): Replaces 'c' \to 'b', 'd' \to 'c', 'e' \to 'd'. No character is replaced more than once. The final string is "abbcd", which matches the expected output.

  • Test case (3): For character 'b', at j=0j=0 it is replaced by 'c'. Then at j=1j=1, since oldc[1] == 'c', it is replaced by 'd'. The result for 'b' becomes 'd' instead of 'c'. Thus, the output is "adcde" instead of "acdde", exposing the flaw.

  • Test case (4): For character 'a', at j=0j=0 it is replaced by 'b'. Then at j=1j=1, since oldc[1] == 'b', it is replaced back by 'a'. Thus 'a' remains 'a' instead of becoming 'b', exposing the flaw.

Therefore, test cases 3 and 4 only will expose the flaw.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q51 of the GATE CSE 2013 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 19 of them are 2-mark MCQs like this one, and GATE CSE 2013 has 2.

How GATE tests Programming in C

More from GATE CSE 2013

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2013 · Q51 · MCQ

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 findandreplace (char *A, char *oldc, char *newc) { for (int i=0; i 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" 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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.