GATE CS 2025 Set 2 | Question 19

Programming & Data Structures › Pointers · MCQ · 1 mark · Answer: D

  1. Question 19Programming & Data Structuresmcq1 mark
    Consider the 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?
    • A.
      @#Hello World!
    • B.
      Hello World!
    • C.
      ello World!
    • D.
      Hello World!d!
      ✓

    Answer: D

    Solution

    Answer: (D) Hello World!d! t=a+2 points at 'H'; s=a. The loop copies *t into *s while *t is non-zero, advancing both. It copies the 12 characters "Hello World!" from a[2..13] into a[0..11]. The loop stops at the null terminator but does NOT copy it, so no new '\0' is written. The old characters a[12]='d', a[13]='!', and the terminator a[14]='\0' remain. Printing a therefore reads a[0..13] = "Hello World!" followed by the leftover "d!" → "Hello World!d!".

More GATE PYQ on Pointers

All GATE CS Programming & Data Structures PYQ →

Attempt GATE CS 2025 Set 2 in real GATE exam mode

Full paper with a timer, the GATE interface and a topic-wise analysis — free.

Create free account →