GATE CSE 2025 Session 2 Q19: Overlapping string copy without the terminating null
- Programming and Data Structures
- 1 mark
- MCQ
- −1/3 if wrong
Question
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?
Try it: multiple choice: exactly one correct option
Wrong answer costs 1/3 mark in GATE.
Show answer and explanation
Answer: D (official GATE 2025 final answer key)
Concept used: The loop stops at the null character without copying it, so the old tail of the array survives.
First step: The 12 characters "Hello World!" are copied two places to the left; the last two original characters "d!" and the original '\0' stay in place.
Full step-by-step solution
Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.
Share this question
Source: official GATE 2025 Session 2 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key
More Programming and Data Structures PYQs
- 2026 Session 1 · Q23Non-leaf indices in a 1-indexed binary heap with n odd
- 2026 Session 1 · Q24Empty slots after linear-probing inserts with h(x) = (x+7) mod 11
- 2026 Session 1 · Q33Maximum height of a full binary tree with 23 nodes
- 2026 Session 1 · Q34Output of a C function with a shadowed block variable
- 2026 Session 1 · Q39Recursive length of a non-empty singly linked list
- 2026 Session 1 · Q40Third insertion that can never yield a complete BST of 1..15