GateAI
Programming in C

GATE CSE 2025 Set 2 · Q19

MCQ1 markMedium

Time

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 ", 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?

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: D) Hello World!d!

Programming in C

Let us trace the execution of stringcopy(a, a + 2):

  1. Initial contents of the array a with its 0-based indices:

    • a[0..13]: '@', '#', 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!'

    • a[14]: '\0'

  2. The function stringcopy(char *s, char *t) is called with s = a (points to a[0]) and t = a + 2 (points to a[2], which is 'H').

  3. The while(*t) loop copies characters from *t to *s until *t points to the null terminator '\0' (a[14]):

    • a[0] <- a[2] ('H')

    • a[1] <- a[3] ('e')

    • a[2] <- a[4] ('l')

    • a[3] <- a[5] ('l')

    • a[4] <- a[6] ('o')

    • a[5] <- a[7] (' ')

    • a[6] <- a[8] ('W')

    • a[7] <- a[9] ('o')

    • a[8] <- a[10] ('r')

    • a[9] <- a[11] ('l')

    • a[10] <- a[12] ('d')

    • a[11] <- a[13] ('!')

  4. At this point, t reaches a[14] which contains '\0'. The loop condition while(*t) evaluates to false and the function terminates.

  5. Crucially, the function does not copy or append a null terminator '\0' to s. Therefore, indices 12 through 14 retain their original values:

    • a[12] is 'd'

    • a[13] is '!'

    • a[14] is '\0'

  6. The final contents of array a up to the null terminator at index 14 are: "Hello World!d!"

Thus, `printf("%s", a);` prints Hello World!d!, matching option (D).

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Programming in C previous year question is Q19 of the GATE CSE 2025 Set 2 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 20 of them are 1-mark MCQs like this one, and GATE CSE 2025 Set 2 has 4.

How GATE tests Programming in C

More from GATE CSE 2025 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2025 Set 2 · Q19 · MCQ

Consider the following C program: #include void stringcopy(char *, char *); int main() { char a[30] = "@#Hello World!"; stringcopy(a, a + 2); printf("%s ", 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?

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.