Similar questions 2 more →
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!.](../../../assets/questions/CS-2025-S2-Q19-1.webp)
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.
![Question 33, GATE 2024: In C, fX(char *a) sets char *b=a, executes while(*b) b++, then returns b−a as int. Which claims are true? A fX("abcd") returns; B with char c[]="abcd", fX(c) returns; C the function does not compile; D with char *c="abcd", fX(c) returns.](../../../assets/questions/CS-2024-S2-Q33-1.webp)
![Question 43, GATE 2022: An ANSI C program initializes int a[3][3][3]={{1,2,3,4,5,6,7,8,9},{10,11,12,13,14,15,16,17,18},{19,20,21,22,23,24,25,26,27}}; int i=0,j=0,k=0; then for(i=0;i<3;i++){for(k=0;k<3;k++) printf("%d ",a[i][j][k]); printf("\n");}. Find the rows printed. A (1,2,3)/(10,11,12)/(19,20,21); B (1,4,7)/(10,13,16)/(19,22,25); C (1,2,3)/(4,5,6)/(7,8,9); D (1,2,3)/(13,14,15)/(25,26,27).](../../../assets/questions/CS-2022-Q43-1.webp)
![Question 43, GATE 2017: C code initializes array[]={3,5,1,4,6,2}, done=0. While done==0, set done=1; for i=0..4, if array[i]<array[i+1], swap them and set done=0; then for i=5..1 descending, if array[i]>array[i-1], swap them and set done=0. After the loop printf("%d",array[3]). swap exchanges the two addressed values. What is printed?](../../../assets/questions/CS-2017-S2-Q53-1.webp)
![Question 11, GATE 2015: What does this C program print?
#include <stdio.h>
int main() { char s1[7]="1234", *p; p=s1+2; *p='0'; printf("%s",s1); }
The assigned character is digit '0', not the null character '\0'. Options: (A) 12; (B) 120400; (C) 1204; (D) 1034.](../../../assets/questions/CS-2015-S3-Q11-1.webp)
![Question 50, GATE 2015: Y is a one-dimensional array of size n and X a C two-dimensional n×n array, with 2≤n≤10. Each segment starts with Y[i]=0 and X[i][j]=i+j. All X elements lie in the same main-memory page frame. Segment 1: for(i=0;i<n;i++) Y[i]+=X[0][i]; Segment 2: for(i=0;i<n;i++) Y[i]+=X[i][0]; Assess S1: final Y contents are identical; S2: X elements accessed in segment 1 are contiguous in memory; S3: X elements accessed in segment 2 are contiguous. Options: (A) S2 only; (B) S3 only; (C) S1 and S2 only; (D) S1 and S3 only.](../../../assets/questions/CS-2015-S3-Q50-1.webp)
![Question 41, GATE 2014: In C, MyX(int *E,unsigned int size) first sets Y=0 and adds every E[i] to Y. It then loops i=0..size−1 and j=i..size−1, initializes Z=0, sums E[k] for k=i..j, and replaces Y by Z if Z>Y. It returns Y. What does it return? A: Maximum sum of a contiguous subarray. B: Maximum element in a subarray. C: Sum of maximum elements of all subarrays. D: Sum of all array elements.](../../../assets/questions/CS-2014-S1-Q51-1.webp)
![Question 10, GATE 2014: For an n×n matrix A, execute C=100; for i=1,…,n and j=1,…,n execute Temp=A[i][j]+C; A[i][j]=A[j][i]; A[j][i]=Temp−C. Then print A in row order. What is printed? A original A; B transpose of A; C upper-diagonal entries increased by 100 and lower-diagonal entries decreased by 100; D none of these.](../../../assets/questions/CS-2014-S3-Q20-1.webp)
![Question 50, GATE 2013: Code loops i=0…4 then j=0…2, and if A[i]==oldc[j] immediately sets A[i]=newc[j], without breaking. Maps are 1 abc→dab;2 cde→bcd;3 bca→cda;4 abc→bac. Testing every 5-character input from{a, b, c, d, e}, how many maps expose its replacement flaw? A1, B2, C3, D4.](../../../assets/questions/CS-2013-Q60-1.webp)
![Question 50, GATE 2013: Code loops i=0…4 then j=0…2, and if A[i]==oldc[j] immediately sets A[i]=newc[j], without breaking. Maps are 1 abc→dab;2 cde→bcd;3 bca→cda;4 abc→bac. Testing every 5-character input from{a, b, c, d, e}, how many maps expose its replacement flaw? A1, B2, C3, D4.](../../../assets/questions/CS-2013-Q60-2.webp)


![Question 33, GATE 2004: What is printed by this C fragment? Assume appropriate declarations/includes for i, strlen and printf.
char p[20];
char *s="string";
int length=strlen(s);
for(i=0;i<length;i++) p[i]=s[length-i];
printf("%s",p);
The %s format follows GFG. GateForum visibly prints a malformed alternate format rather than %s.
Options:
(A) gnirts
(B) string
(C) gnirt
(D) No output is printed](../../../assets/questions/CS-2004-Q33-1.webp)
![Question 2.8, GATE 2002: C declares char a[100][100]. Memory is byte-addressable, and the array starts at address 0. What is the address of a[40][50]?
(A) 4040; (B) 4050; (C) 5040; (D) 5050.](../../../assets/questions/CS-2002-Q33-1.webp)