GATE 2016 C Programming Questions: Topic Map and Worked Traces

Trace caller state, shared static storage and array recurrences in selected GATE 2016 questions. Use the worked answers to identify what to practise next.

KnowledgeGate Team

Exam prep & CS education

Updated 30 Nov 20265 min read
GATE 2016 C Programming Questions: Topic Map and Worked Traces

Remembering C syntax does not guarantee that you can track what changes during a function call. Six selected GATE 2016 C questions, four from Set 1 and two from Set 2, expose that distinction through three worked traces and a revision order.

Where can you find the original papers?

IIT Roorkee's official previous-paper archive lists a bulk archive for 2007 to 2024 for original-paper checking. Open the relevant set before attempting a question, and keep its declarations beside the code. In C, a single * changes what an expression means: p is a pointer value, while *p accesses the object it points to. Copy the initial values and the exact expression being printed before starting your trace. This separates a reading mistake from a tracing mistake when you compare your answer.

Use this analysis alongside GATE, PSU, Engineering & Technical Exams when organising wider preparation.

Topic-wise distribution

Each question appears once in these skill buckets. Recursion can involve arrays without earning a second count.

Skill bucket

Question count

Pointer parameters and caller state

2

Function-argument types

1

Array loop invariant

1

Recursion and persistent state

2

Practise state tracing, argument types and loop termination together.

Pointer arguments: changing a pointee versus swapping local pointers

The GATE 2016 Set 2 pointer-parameter question uses this function:

c
void f(int *p, int m) {
    m = m + 5;
    *p = *p + m;
    return;
}

The caller starts with int i=5, j=10;, calls f(&i,j), then prints i+j.

(GATE 2016 Set 2, see the solved trace)

  1. On entry, p points to i; local m=10.

  2. m=10+5=15 changes only the parameter.

  3. *p=5+15=20 changes caller variable i.

  4. Caller j remains 10, so i+j=20+10=30.

C passes both arguments by value. The copied pointer still designates caller storage.

The earlier GATE 2015 C Programming Questions: Topic Map and Worked Traces owns value-parameter swaps and caller updates; this 2016 set isolates pointer reassignment, shared static storage and array invariants.

Contrast the GATE 2016 Set 1 pointer-swap question: mystery executes only temp=ptrb; ptrb=ptra; ptra=temp;. Start with a=2016,b=0,c=4,d=42 and follow the addresses separately from the stored integers:

  1. In mystery(&a,&b), local ptra points to a and local ptrb points to b.

  2. temp=ptrb saves the address of b. It does not copy the integer 0 into a.

  3. ptrb=ptra makes ptrb point to a; ptra=temp makes ptra point to b. Neither statement assigns through a pointer, so a=2016 and b=0 remain unchanged.

  4. Back in the caller, a<c is 2016<4, false. Therefore, mystery(&c,&a) is skipped.

  5. The final mystery(&a,&d) swaps another pair of local pointer values. Again no pointee changes, so the output is 2016.

To exchange the caller's integers, the assignments would need to write to the pointed-to objects. Merely redirecting local pointers cannot perform that exchange.

Draw the distinction using Pointers in C for GATE: Memory Diagrams and Output Traces.

Static storage in recursion: one d, three n values

Recursion frames share static d, producing entry pairs 31, 22, 13 and output 312213444.

For the GATE 2016 Set 1 static-storage question, start with count(3):

c
void count (int n) {
    static int d=1;
    printf ("%d",n);
    printf ("%d",d);
    d++;
    if (n>1) count (n-1);
    printf ("%d",d);
}

(GATE 2016 Set 1, see the solved trace)

The entry pairs (n,d) are (3,1), (2,2) and (1,3). Their increments move d from 1 to 2, then 3, then 4. Each call has its own n, but all calls share the single static d; its initialiser does not run again on recursive entry.

Unwinding prints 4 three times. Joining the entry prints 31, 22, 13 and return prints 444 gives 312213444, with no spaces.

Diagram of count(3), count(2), and count(1) sharing static d from 1 to 4, with return prints of 4 and output 312213444.

Array questions: trace the recurrence and preserve the invariant

The GATE 2016 Set 2 recursive-array function returns 0 for n<=1; otherwise it returns max(f(p+1,n-1),p[0]-p[1]). For {3,5,2,6,4} and n=5, adjacent differences are 3-5=-2, 5-2=3, 2-6=-4, 6-4=2.

(GATE 2016 Set 2, see the solved trace)

Unwind from 0: max(0,2)=2, max(2,-4)=2, max(2,3)=3, max(3,-2)=3. Answer: 3, the largest adjacent drop with a zero floor, not the array maximum 6 or an unrestricted pairwise difference.

The GATE 2016 Set 1 array-loop question starts a=0,b=n-1, discards the smaller endpoint and returns p[a]. Its guard options are:

  • (a) a != n

  • (b) b != 0

  • (c) b > (a + 1)

  • (d) b != a

Answer (d), b != a, leaves one candidate. A maximum survives each discard. Option (c) stops too early: {3,5} returns 3 without entering the loop.

Try the loop on another array: {3,5,2,6,4} gives endpoint pairs (0,4)→(1,4)→(1,3)→(2,3)→(3,3), returning p[3]=6. The comparisons are 3<=4 (true), 5<=4 (false), 5<=6 (true) and 2<=6 (true). Each step shrinks the interval by one. The surviving interval still contains a maximum because the discarded endpoint has a remaining endpoint at least as large. For n=1, zero iterations correctly return p[0].

Difficulty comes from the state you forget to track

Pointer copies invite misreading, static recursion requires call order, and the array loop requires an invariant. Before evaluating an expression, mark which variables belong to the caller, which belong to one call and which persist across calls. This small habit makes each update visible and prevents an otherwise correct calculation from using the wrong value.

The GATE 2016 Set 1 function-argument question gives void f(int,short) and int i=100; short s=12; short *p=&s;:

  • (a) f(s,*s)

  • (b) i = f(i,s)

  • (c) f(i,*s)

  • (d) f(i,*p)

Answer (d) matches the prototype: i supplies an int, and dereferencing p supplies the short value stored in s.

  • Option (a) fails because *s applies indirection to a short, although passing s as the first argument would allow conversion to int.

  • Option (b) fails because f returns void, so there is no result to assign to i.

  • Option (c) fails because its second argument again dereferences s, which holds an integer, not an address.

Strengthen prerequisites with C Programming for GATE: A 6-Week Topic-by-Topic Plan.

GATE 2016 C revision: three ledgers, one timed pass

Use one 45-minute block to reproduce the three state models:

  • 10 minutes: redo pointer-state traces.

  • 15 minutes: redraw static-recursion and adjacent-difference traces.

  • 10 minutes: prove array-loop termination, including n=1.

  • 10 minutes: log errors and retry without notes.

For a broader sequence, use GATE Guidance by Sanchit Sir. Before checking an answer, write the static d ledger and the endpoint pairs for {3,5,2,6,4}; log which state you lost.