IAE Academy

GATE 2025 CS2 Q.19: solution and answer

GATE 2025 · CS2Q.19 · 1 mark · MCQ

Consider the following C program:

#include <stdio.h>
void stringcopy(char *, char *);
int main(){
    char a[30] = "@#Hello World!";
    stringcopy(a, a + 2);
    printf("%s\n", a);
    return 0;
}
void stringcopy(char *s, char *t) {
    while(*t)
        *s++ = *t++;
}

Which ONE of the following will be the output of the program?

  1. (A)

    @#Hello World!

  2. (B)

    Hello World!

  3. (C)

    ello World!

  4. (D)

    Hello World!d!

See the question in the paper

Answer and solution

ANSWER

The correct answer is (D) Hello World!d!

Official answer: DSource: Official answer key, GATE 2025 CS2 (IIT Roorkee)

  1. The array is initialised: a[0..14] = '@','#','H','e','l','l','o',' ','W','o','r','l','d','!','\0'.

  2. stringcopy(a, a+2) sets s = &a[0] (destination, trailing) and t = &a[2] (source, leading). Since s is always exactly 2 positions behind t, the forward loop never overwrites a byte before t has already read it — the overlapping shift is safe.

  3. Trace of while(*t) *s++ = *t++; (writing a[i] = original a[i+2] for i = 0..11): a[0]='H', a[1]='e', a[2]='l', a[3]='l', a[4]='o', a[5]=' ', a[6]='W', a[7]='o', a[8]='r', a[9]='l', a[10]='d', a[11]='!'.

  4. Next check: *t at t = &a[14], whose ORIGINAL value is '\0' — the while-condition is tested before any assignment, so the loop exits immediately and s never writes to index 12.

  5. Therefore a[12] and a[13] keep their pre-shift original values, 'd' and '!', and a[14] is still the original '\0' — none of these three positions were ever touched by the copy.

  6. Full contents of a up to the terminator: a[0..11] = "Hello World!", followed by a[12]='d', a[13]='!', a[14]='\0'.

  7. printf("%s\n", a) prints from a[0] until the '\0' at index 14, producing "Hello World!d!" — option (D).

KEY IDEA

stringcopy(a, a+2) performs a forward, overlapping left-shift of the string by 2 positions. Because the destination pointer s always trails the source pointer t by exactly 2 (i.e., s is always behind t), each byte is read by t before s ever reaches that same array position to overwrite it — so a forward while(*t) *s++ = *t++; loop is safe here (no data is clobbered before being read). The loop stops the instant *t is the ORIGINAL null terminator, so any bytes beyond where s has reached at that point retain their pre-shift values rather than being copied or zeroed.

WATCH OUT FOR

Assuming the loop runs one extra time and copies the null terminator (or copies through index 13), which would wipe out the leftover 'd' and '!' and give "Hello World!" (option B). The loop condition while(*t) is checked BEFORE each assignment, so as soon as t points at the original '\0' (index 14), the loop exits immediately without ever writing to s at that position — leaving a[12] and a[13] (originally 'd' and '!') untouched from before the shift. Another common slip is forgetting these are the original pre-shift values at those two positions, not garbage or copies of something else.

From the GATE 2025 CS2 paper. Every question of this paper · Download the full paper · All 11 Pointers & arrays questions, with notes

More Pointers & arrays questions