GATE / CS ARCHIVE

GATE CSE 2025 SET 2 / QUESTION 19

GATE CSE 2025 Set 2 Q19

Programming in C · Arrays & Strings · Full GATE CSE 2025 Set 2 paper

GATE 2025 Set 2, Q19 1 mark · MCQ
Report issue

Arrays & Strings

Question 19, GATE 2025: A C program initializes char a[30]="@#Hello World!"; calls stringcopy(a,a+2); and prints a using printf("%s\n",a). The user-defined function is:
void stringcopy(char *s,char *t) { while (*t) *s++ = *t++; }
What is printed before the newline?
Options: A. @#Hello World!; B. Hello World!; C. ello World!; D. Hello World!d!.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Step through it (34 steps)

The source pointer begins at index 2, the H, and the destination begins at index 0. Initially the string occupies indices 0 through 13 and its null terminator is at index 14. The loop copies the 12 non-null characters “Hello World!” from source indices 2…13 into destination indices 0…11.

After the last exclamation mark is copied, t points at index 14. The condition *t is now zero, so the loop stops before copying that null terminator. Destination indices 12 and 13 were never overwritten and still contain the original d and !. The first terminating null remains at index 14. Thus the complete printed sequence is “Hello World!d!”, followed by the newline: D.

The key mistake in the function is failing to copy or append '\0'. This is an explicit forward-copy loop that reads two positions ahead of its writes, so previously overwritten bytes do not corrupt later reads. It should not be confused with calling the standard strcpy on overlapping arrays, which would have undefined behavior.

Original paper, page 17 · Answer key