GATE 2015 C Programming Questions: Topic Map and Worked Traces

Knowing a C rule is only the start. Trace pointer updates, recursion and persistent state in GATE 2015 questions, then use the topic map to plan revision.

KnowledgeGate Team

Exam prep & CS education

Updated 24 Nov 20264 min read
GATE 2015 C Programming Questions: Topic Map and Worked Traces

You may remember how pointers and recursion work, yet lose track when an unfamiliar C program combines several rules. The solution is to record each state change instead of guessing the output from a familiar pattern. A state table for each trace keeps the changed object and the resulting output visible.

What the published 2015 sample covers

The 14-question sample and its year/set labels come from KnowledgeGate's published GATE CS 2015 records; confirm set labels against the official GATE 2015 CS question papers archived by IIT Kanpur.

The table groups the 14 questions by the C rule each one exercises: arrays and pointers dominate, while functions, control flow, and storage examples supply the remaining traces.

Topic group

Questions

Arrays & Pointers

7

Functions

3

Control Flow

2

Storage Classes, Structures & Enums

2

Total

14

Arrays and pointers test which object an expression reaches and changes. Functions test parameter passing, recursive return values and call counts. Control flow tests integer arithmetic and switch fall-through. The storage examples test local, global and static state across calls.

Use the map alongside the GATE, PSU, Engineering & Technical Exams to choose practice themes.

Value parameters versus pointer parameters

C swap trace comparing copied value parameters with pointers that change caller variables.

Consider the two-swap-functions question. (GATE 2015 Set 1, see the solved page)

The caller starts with a=4, b=5, c=6. In f1(int a,int b), the statements c=a; a=b; b=c; use a local int c and swap only the copied parameters. In f2(int *a,int *b), the statements c=*a; *a=*b; *b=c; swap the pointed-to objects.

Stage

Caller's (a,b,c)

Before either swap

(4,5,6)

After f1(a,b)

(4,5,6)

After f2(&b,&c)

(4,6,5)

Each function's local c is separate from main's c. Only dereferencing the pointers in f2 reaches main's b and c. Therefore, printf("%d",c-a-b) computes 5-4-6 = 1-6 = -5.

C passes every argument by value. A copied pointer still identifies a caller object. Keep local and caller variables in separate rows, even in this short trace, to avoid swapping the wrong objects.

Pointer movement and exact printed output

Now trace this pointer-array question. (GATE 2015 Set 3, see the solved page)

c
static int a[]={10,20,30,40,50};
static int *p[]={a,a+3,a+4,a+1,a+2};
int **ptr=p;
ptr++;
printf("%d%d",ptr-p,**ptr);

ptr++ advances by one element of p, whose elements are pointers. Track the levels separately:

Expression after increment

Value

ptr

&p[1]

ptr-p

1

*ptr

a+3

**ptr

a[3], or 40

The intended exam output is 140, because the format contains no space between the conversions. Writing 1 40 changes the output.

There is also a type issue: pointer subtraction produces ptrdiff_t. The portable format here is %td%d; %d is not correct for ptrdiff_t on every platform. Separate the intended arithmetic answer from the original format's portability problem.

Recursion: returned values and invocation counts

Recursion worksheet showing F values alongside invocation and print counts for get(6).

The Set 2 return-value question uses this function:

c
int fun(int n){
    int x=1,k;
    if(n==1)return x;
    for(k=1;k<n;++k) x=x+fun(k)*fun(n-k);
    return x;
}

Write F(1)=1 and F(n)=1+sum(F(k)*F(n-k)) for k=1 through n-1. Then:

  • F(2)=1+1=2.

  • F(3)=1+2+2=5.

  • F(4)=1+5+4+5=15.

  • F(5)=1+15+10+10+15=51.

The initial 1 belongs to every non-base call, not just the outermost call.

The Set 3 invocation-count question asks something different:

c
void get(int n){
    if(n<1)return;
    get(n-1);
    get(n-3);
    printf("%d",n);
}

Let T count all invocations and P count print executions. For n<1, T(n)=1 but P(n)=0. Otherwise, each satisfies Q(n)=1+Q(n-1)+Q(n-3).

n

T(n): invocations

P(n): prints

1

3

1

2

5

2

3

7

3

4

11

5

5

17

8

6

25

12

Thus get(6) makes 1+17+7=25 invocations, including itself, but prints 1+8+3=12 times. Define what you are counting before choosing the base case.

Control flow and persistent state need separate ledgers

In the Set 3 control-flow question, j=2*3/4+2.0/5+8/5 gives 1+0.4+1=2.4: both 6/4 and 8/5 use integer division. Assignment to integer j gives 2. Next, k=0; k-=--j; leaves j=1, k=-1.

For i=0..4, the switch receives -1,0,1,2,3. Cases 1 and 2 share a print, followed by a case-3 print and a default print, with no breaks.

Switch value

Prints executed

-1

1

0

1

1

3

2

3

3

2

Total: 1+1+3+3+2=10.

For the Set 3 storage question, keep separate objects: main's local x=1; f1's local x goes from 25 to 26; f2's static x contributes 51 and 52; f3 changes global x from 10 to 100. The sum is 1+26+51+100+52=230. This does not require left-to-right evaluation of +: the static-call contributions total 103 whichever executes first.

Turn the topic map into a revision session

Start with the swap trace and the pointer-array trace. On paper, give every variable or pointer its own row, then write one line for each recursive return and each switch value. Finish by explaining one wrong transition aloud before checking the result. This order keeps object identity, call count, and persistent state as separate error categories.

For pointers, track object identity and type; for recursion, write a recurrence; for storage, keep separate state ledgers. Use the C Programming for GATE: A 6-Week Topic-by-Topic Plan to spread that revision across topics.

What to carry into your next practice set

Identify which object changes, write the recursion quantity being counted, and reproduce the exact format string. If errors keep returning, use the C Language course to rebuild the rules behind them, then solve the traces again without looking at the answers.