GATE CS 2025 Set 2 | Question 19
Programming & Data Structures › Pointers · MCQ · 1 mark · Answer: D
- Question 19Programming & Data Structuresmcq1 markConsider the C program:
Which ONE of the following will be the output of the 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++; }- A.@#Hello World!
- B.Hello World!
- C.ello World!
- D.Hello World!d!✓
Answer: D
Solution
Answer: (D) Hello World!d!t=a+2points at 'H';s=a. The loop copies*tinto*swhile*tis non-zero, advancing both. It copies the 12 characters "Hello World!" froma[2..13]intoa[0..11]. The loop stops at the null terminator but does NOT copy it, so no new '\0' is written. The old charactersa[12]='d',a[13]='!', and the terminatora[14]='\0'remain. Printingatherefore readsa[0..13]= "Hello World!" followed by the leftover "d!" → "Hello World!d!". - A.
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 →