GATE 2013 C Programming Questions: Topic Breakdown and Worked Traces

Track one shared variable through recursion, then catch repeated character replacements. Three selected GATE 2013 questions show how to diagnose mistakes with state tables.

KnowledgeGate Team

Exam prep & CS education

Updated 26 Nov 20264 min read
GATE 2013 C Programming Questions: Topic Breakdown and Worked Traces

These three GATE 2013 questions test changing state: one shares a reference through recursion, while two expose replacement cascades. State tables and a targeted error-repair loop turn each answer into a reusable method.

The earlier GATE 2015 C Programming Questions: Topic Map and Worked Traces owns the broad pointer, recursion, control-flow and storage sweep. This GATE 2013 analysis narrows to shared-reference recursion and replacement cascades, so use it for state mutation and witness construction.

Distinguish C arrays from C++ references

The GATE 2013 paper is available in IIT Kanpur's official GATE archive.

The replacement procedure uses C-style arrays, but the recursion question uses int &x, which is C++ reference syntax. Standard C does not have native reference parameters. A reference makes x another name for the caller’s variable; changing x changes that variable. The value parameter c gives each call a separate counter.

Use the broader GATE preparation resources to place this exercise within your preparation.

Topic distribution: shared state and character replacement

Topic distribution across three GATE 2013 questions

Teaching group

Questions

Main task

Recursion with a shared reference

1

Track one shared variable and separate counters

Character replacement and test design

2

Trace mutation and find counterexamples

Total

3

Explain each state change

The replacement questions share one procedure: one asks which mappings fail for abcde; the other tests every five-character input.

For revision, treat both groups as moderate: recursion needs a clear aliasing model, while replacement needs careful comparisons after assignment.

Recursion: one shared variable, several local counters

The GATE 2013 reference-recursion question asks for f(p,p) with p=5:

cpp
int f (int &x, int c) {
    c = c - 1;
    if (c==0) return 1;
    x = x + 1;
    return f(x,c) * x;
}

Every x refers to the same p. Each call has its own value parameter c.

Entry c

c after decrement

Shared p before next call or return

5

4

6

4

3

7

3

2

8

2

1

9

1

0

9, return 1

Under the intended recursive-result-first trace, the four pending multiplications all read 9:

1 x 9 = 9, then 9 x 9 = 81, then 81 x 9 = 729, then 729 x 9 = 6561.

The intended answer is (b), 6561. Do not multiply separate remembered values 6, 7, 8 and 9: the frames share one variable.

However, multiplication does not impose evaluation order between its operands. The printed expression does not guarantee this trace portably. Compute the recursive result before multiplying:

cpp
int r = f(x,c);
return r * x;

For further practice, recursion with pointers develops shared-state tracing using pointer syntax.

Character replacement: trace the value after every assignment

Trace case 3 replacing b twice, showing intended c versus actual d, plus case 4 returning a to a.

The GATE 2013 character-replacement question uses this flawed procedure:

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

For A="abcde", each character visits all three j values. The intended behaviour replaces each original character once; the actual loop tests the updated character again.

Case

oldc

newc

Intended

Actual

Exposes flaw?

1

abc

dab

dabde

dabde

No

2

cde

bcd

abbcd

abbcd

No

3

bca

cda

acdde

addde

Yes

4

abc

bac

bacde

aacde

Yes

Follow case 3 at A[1]='b': j=0 writes c; j=1 now matches that c and writes d; j=2 compares d against a and does nothing. Intended c becomes actual d.

Case 3 shows one character changing from b to c to d during replacement.

Case 4 changes the original a to b, then back to a. Therefore (c), 3 and 4 only, exposes the flaw.

Exhaustive inputs do not change which mappings are flawed

The GATE 2013 exhaustive-input question allows every five-position string over {a,b,c,d,e}, including duplicates. There are 5 x 5 x 5 x 5 x 5 = 3125 possible inputs per mapping.

A cascade requires a newly written character to match at a later j:

  • Case 1: a becomes absent d; b becomes a at an already passed index; c changes at the last index.

  • Case 2: c becomes absent b; d becomes c at an already passed index; e changes at the last index.

  • Case 3: b becomes c, then d. Witness bbbbb: intended ccccc, actual ddddd.

  • Case 4: a becomes b, then a. Witness aaaaa: intended bbbbb, actual aaaaa.

Thus (b), Only two, counts faulty mapping cases, not input arrays. With unique oldc entries, insert break immediately after a successful replacement to repair the procedure.

Repair each tracing error with a state ledger

Run this error-repair loop:

Error pattern

Diagnostic action

Reference recursion

Draw one shared p column and one local c row per call; recompute the pending products.

Replacement cascade

Replay every j after an assignment and record the first changed character.

Exhaustive mapping

Build one witness string for each suspect mapping and separate mapping count from input count.

Answer checking

Solve once without options, then explain the first incorrect state transition.

Repeat the loop until you can name the state change that caused the wrong answer. The topic-by-topic C revision plan provides a wider sequence when these three traces expose a broader gap.

The next step depends on the error you made

Explain why p ends at 9, why case 3 produces addde, and why exhaustive testing still gives “Only two”. An answer without these transitions leaves the underlying mistake unresolved.

If these explanations remain difficult, the C Language course offers basic-to-advanced concepts, MCQs and coding practice for rebuilding the foundation. Reattempt these questions with a state table before checking the answers.