GATE / CS ARCHIVE

SUBJECT COLLECTION

Programming in C · Arrays & Strings

12 questions, newest first

GATE CSE has asked 12 questions on Arrays & Strings (Programming in C) from 2002 to 2025, in 10 of the 46 papers; the most recent is in GATE CSE 2025 Set 2. In the last five years it carried 0.5 marks per 100-mark paper on average, against 0.5 since 2003. Since 2014 the questions have been 6 MCQ, 1 MSQ and 1 NAT. 3 groups of questions here are solved the same way; “One per method” keeps the newest of each.

Browse topics
1 mark · MCQ
Report issue

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!.
Tap to enlarge
Your answer
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.

Original paper, page 17 · Answer key

1 mark · MSQ
Report issue

A read-only string-length loop

Arrays & Strings

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.
Tap to enlarge
Select all that apply
Answer & solution

Official answer: A, B, D

Approach: Trace the pointer until the null terminator and distinguish reading a literal from modifying it.

1. Derivation. The pointer b advances until it reaches the terminating null byte and returns the element difference b−a, the string length. Each stated argument is a valid null-terminated four-character string, so each call returns 4. The function only reads characters; it does not modify string literals. Therefore A, B, D are true and the code is valid C. Pointer subtraction here is within the same array and the small result fits int.

2. Explanation and checks. The bytes at offsets 0,1,2,3 are a,b,c,d and are nonzero, so the loop advances four times. At offset 4 the terminating zero makes the condition false. Both pointers remain associated with the same character array, so subtraction gives four elements. The function never assigns through either pointer, and the supplied arguments are terminated, avoiding two common string errors. These are C examples; a stricter C++ string-literal type rule is not the language specified.

Options A, B and D print the string literal abcd between typographic quotation marks (”abcd”). These are read as ordinary ASCII string delimiters, and under that intended reading each call returns 4. The function definition itself contains no quotation marks and is valid C as printed; only the quote glyphs in the options, copied literally, would not form a C string literal.

Original paper, page 22 · Answer key

2 marks · MCQ
Report issue

Arrays & Strings

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).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

The initializer fills the 3×3×3 array in row-major order with 1 through 27. The middle index is fixed at j=0, while i and k vary. The selected values are a[0][0][k]=1, 2, 3; a[1][0][k]=10, 11, 12; a[2][0][k]=19, 20, 21. Thus the output is option A.

The address/value formula for this initializer is a[i][j][k]=9i+3j+k+1. Since j never changes from zero, each outer iteration jumps nine values, while each inner iteration advances one. The newlines occur after the three k values, producing exactly three rows.

Original paper, page 28 · Answer key

2 marks · NAT
Report issue

Arrays & Strings

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?
Tap to enlarge
Answer & solution

Official answer: 3.0

Our answer: 3

Step through it (32 steps)

1. Both passes correct adjacent pairs toward descending order. In the forward pass a smaller value on the left is swapped right; in the backward pass a larger value on the right is swapped left. The initial forward pass changes [3,5,1,4,6,2] into [5,3,4,6,2,1].

2. Its backward pass produces [6,5,3,4,2,1]. In the next forward pass the remaining out-of-order pair 3,4 swaps, producing [6,5,4,3,2,1]; the backward pass then makes no changes.

3. Because a swap occurred, done remains 0 and one more full iteration verifies that no swaps are needed. Every strict adjacent inversion swap decreases a finite inversion count, so termination is justified. The final zero-based index 3 contains 3, which is printed. Index 3 means the fourth element, not the third.

Original paper, page 17 · Answer key

1 mark · MCQ
Report issue

Arrays & Strings

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.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Step through it (8 steps)

1. Initially the character array begins with 1,2,3,4 followed by a null terminator. Pointer arithmetic advances by characters, so s1+2 selects the third stored character. The assignment changes that position only; printf with %s stops at the unchanged original terminator.

2. p points to s1[2]. Assigning character'0' replaces digit 3, not the terminating null byte. The string becomes 1204 followed by its original terminator, so C.

Original paper, page 57 · Answer key

2 marks · MCQ
Report issue

Arrays & Strings

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.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. Both loops add i to initially zero Y[i], because X[0][i]=0+i and X[i][0]=i+0.

2. In C row-major storage, X[0][i] traverses consecutive elements, whereas X[i][0] advances by n elements for n≥2. Being in one page does not make those column elements adjacent. Thus S1, S2, C.

Original paper, page 71 · Answer key

2 marks · MCQ
Report issue

Arrays & Strings

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.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

1. Method and setup. Each ordered index pair i≤j describes one nonempty contiguous subarray. The innermost loop computes its sum from scratch. The running variable Y retains the largest sum seen; initializing it to the whole-array sum ensures a valid candidate even when every entry is negative, unlike initializing the final maximum to zero.

2. Work through the result and its assumptions. Y starts as the sum of the whole array, itself a valid nonempty contiguous subarray. Every pair i≤j is then enumerated and Z sums exactly E[i..j]; Y is replaced whenever this sum is larger. Thus the final Y is the maximum nonempty subarray sum, A. This assumes a nonempty valid array, representable indices and no signed arithmetic overflow; for an empty array the routine returns 0.

Original paper, page 18 · Answer key

1 mark · MCQ
Report issue

Arrays & Strings

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.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step 1: Track one iteration with old values u=A[i][j] and v=A[j][i]. Temp=u+100, then the assignments store v and (u+100)−100=u, so the body swaps the two entries. Step 2: For i≠j, both index pairs (i,j) and (j,i) occur in the complete nested loops. The second swap undoes the first. A diagonal entry is swapped with itself. Step 3: Consequently every entry finishes unchanged, A. This reasoning uses exact arithmetic or assumes no problematic overflow in adding and subtracting C.

Original paper, page 55 · Answer key

2 marks · MCQ
Report issue

Which replacement maps expose cascading substitutions

Arrays & Strings

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.
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.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

The defect is that the inner loop continues after a match, so a replacement character can match a later oldc entry and be replaced again. In case 1 (abc→dab), a becomes d, b becomes a after the a-entry has already been passed, and c becomes b after the b-entry has been passed. No input character cascades. In case 2 (cde→bcd), each replacement is absent from the remaining suffix of oldc, so again no character cascades. In case 3 (bca→cda), an original b first becomes c and then becomes d; the intended one-step result is c. In case 4 (abc→bac), an original a first becomes b and then becomes a; the intended result is b. Exhaustive length-five input testing includes strings containing b and a, respectively, so exactly cases 3 and 4 can reveal the flaw. That is two cases, option B. The five-character array need not be NUL-terminated because the code accesses exactly its five given elements.

The intended specification performs one replacement based on the original character, whereas the code repeatedly tests the updated character. For each map, inspect whether a replacement equals an oldc entry at a later j. Only later entries matter: a match with an earlier entry is never revisited. Map3 has b→c with c later, and map 4 has a→b with b later. Maps 1and2 only map backward or outside the remaining entries, so no repeated substitution can occur. Exhaustive input testing guarantees witnesses containing the affected characters.

Original paper, page 11 · Answer key

2 marks · MCQ
Report issue

Replacement-map failures on abcde

Arrays & Strings

Question 51, GATE 2013: Use the same nested replacement loops and four maps 1 abc→dab;2 cde→bcd;3 bca→cda;4 abc→bac. For input abcde specifically, which expose the flaw? A none; B2 only; C3and4; D4 only.
Question 51, GATE 2013: Use the same nested replacement loops and four maps 1 abc→dab;2 cde→bcd;3 bca→cda;4 abc→bac. For input abcde specifically, which expose the flaw? A none; B2 only; C3and4; D4 only.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

Apply each map once to each original character to obtain the intended result, then compare with the procedure’s sequential replacements. For input abcde, case 1 has intended and actual output dabde. Case 2 has intended and actual output abbcd. Case 3 should produce acdde, but its b is changed twice, b→c→d, producing addde. Case 4 should produce bacde, but its initial a is changed twice, a→b→a, producing aacde. Therefore cases 3 and 4 expose the defect, option C.

The fixed input contains every candidate letter once, including both b and a that trigger the two flaws. In map 3 the intended b→c is followed by an unintended c→d, changing the second character from expected c to d. In map 4 the intended a→b is followed by unintended b→a, undoing that substitution. Comparing complete strings confirms the discrepancy rather than relying merely on the presence of an intermediate match. Cases 1and2remain unaffected.

Original paper, page 11 · Answer key

2 marks · MCQ
Report issue

Copying the terminator before reversed characters

Arrays & Strings

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
Tap to enlarge
Your answer
Answer & solution

Answer: D under the displayed intended C fragment (no official key was published)

1. The source length is 6. At i=0, p[0]=s[length−0]=s[6], which is the string terminator.

2. Consequently printf with %s sees an empty string immediately, regardless of later copied characters, and prints nothing, D. This uses the paper’s %s format and assumes surrounding declarations/includes for i and the library functions.

4. Teaching check: The source string positions are 0:s,1:t,2:r,3:i,4:n,5:g,6:\0. The very first assignment copies position 6 into p[0].

Original paper, page 4

2 marks · MCQ
Report issue

Address of an element of a C array

Arrays & Strings

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.
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

Step 1: Compute the linear index before the byte address.

Step 2: There are 40 complete rows before row40, each contributing 100 characters, and 50 earlier characters within that row. Therefore the zero-based linear index is 4000+50=4050. Since sizeof(char)=1, no further scale factor is needed. The5040 alternative reverses the row and column roles.

Conclusion: C lays out rows contiguously in row-major order. Each char occupies one byte, and each row has 100 elements. The offset of a[40][50] is 40×100+50=4050 bytes, giving address 4050 from the stipulated zero base, B.

Original paper, page 4