Strings MCQs in C Language
1.
Output:
#include <stdio.h>
int main() {
char str[5] = "CABC";
printf("%c", str[4]);
}
A) C
B) Null character (\0)
C) A
D) Compiler error
✅ Answer: B
Explanation:
C strings are null-terminated → str[4] = \0.
2.
Which of the following is invalid?
A) char str[] = "GATE";
B) char str[5] = "CABC";
C) char str[3] = "CABC";
D) char str[6] = "HELLO";
✅ Answer: C
Explanation:
Array size too small → string + null character won’t fit → invalid.
3.
Output:
#include <stdio.h>
int main() {
char str[] = "GATE";
printf("%c", str[2]);
}
A) G
B) A
C) T
D) E
✅ Answer: C
Explanation:
str[2] → third character → T.
4.
Which function is used to find the length of a string?
A) strlen()
B) strsize()
C) sizeof()
D) strlength()
✅ Answer: A
Explanation:strlen() returns number of characters excluding null character.
5.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "CABC";
char str2[] = "CABC";
printf("%d", strcmp(str1, str2));
}
A) 0
B) 1
C) -1
D) Garbage
✅ Answer: A
Explanation:strcmp() returns 0 when strings are equal.
6.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "C";
char str2[] = "ABC";
strcpy(str1, str2);
printf("%s", str1);
}
A) C
B) ABC
C) AB
D) Compiler error
✅ Answer: B
Explanation:strcpy() copies source to destination → str1 = “ABC”.
7.
Which function concatenates two strings?
A) stradd()
B) strcat()
C) strmerge()
D) strappend()
✅ Answer: B
Explanation:strcat(dest, src) appends src at end of dest.
8.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "GATE";
printf("%lu", strlen(str));
}
A) 3
B) 4
C) 5
D) 6
✅ Answer: B
Explanation:
“GATE” → 4 characters (null not counted).
9.
Output:
#include <stdio.h>
int main() {
char str[10] = "CABC";
str[2] = 'X';
printf("%s", str);
}
A) CAXC
B) CABC
C) CXBC
D) Compiler error
✅ Answer: C
Explanation:
str[2] modified → C (0) A (1) X (2) C (3) → “CXBC”.
10.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "GAT";
char str2[] = "EG";
strcat(str1, str2);
printf("%s", str1);
}
A) GATE
B) GATG
C) GATEG
D) Compiler error
✅ Answer: A
Explanation:strcat() appends str2 → str1 = “GATEG” (Correction: careful: str1 = “GAT” + “EG” → “GATEG”).
✅ Corrected Answer: C
11.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "CABC";
char str2[10];
strcpy(str2, str1);
printf("%s", str2);
}
A) CABC
B) CAB
C) ABC
D) Compiler error
✅ Answer: A
Explanation:strcpy() copies str1 → str2 = “CABC”.
12.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "CABC";
char str2[] = "CABC";
printf("%d", strcmp(str1, "CAB"));
}
A) 0
B) Positive number
C) Negative number
D) Compiler error
✅ Answer: B
Explanation:
str1 > “CAB” → strcmp returns positive value.
13.
Which of the following is a correct string declaration?
A) char str[] = 'CABC';
B) char str[] = "CABC";
C) char str[4] = "CABC";
D) char str[3] = "CABC";
✅ Answer: B
Explanation:
Strings must use double quotes → option B is correct.
14.
Output:
#include <stdio.h>
int main() {
char str[5] = "ABCD";
printf("%c", str[4]);
}
A) D
B) Null character
C) C
D) Compiler error
✅ Answer: B
Explanation:
str[4] = ‘\0’ → null character.
15.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
printf("%c", str[strlen(str)-1]);
}
A) G
B) A
C) T
D) E
✅ Answer: D
Explanation:
strlen(str) = 4 → last index = 3 → str[3] = E.
16.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "GAT";
char str2[] = "EG";
strcat(str1, str2);
printf("%s", str1);
}
A) GAT
B) GATEG
C) GATE
D) Compiler error
✅ Answer: B
Explanation:
str1 = “GAT” + “EG” → “GATEG”.
17.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
printf("%d", strlen(str)+1);
}
A) 4
B) 5
C) 6
D) 3
✅ Answer: B
Explanation:
strlen(str) = 4 → +1 for null character → 5.
18.
Which function compares two strings?
A) strcompare()
B) strcmp()
C) strcmpi()
D) streq()
✅ Answer: B
Explanation:strcmp() → compares strings, returns 0 if equal.
19.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "CABC";
str[0] = 'X';
printf("%s", str);
}
A) XABC
B) CABC
C) XXBC
D) Compiler error
✅ Answer: A
Explanation:
First character replaced → “XABC”.
20.
Which of the following is true about C strings?
A) Always end with null character \0
B) Can store multiple types
C) Can be directly compared using ==
D) Cannot be passed to functions
✅ Answer: A
Explanation:
C strings are null-terminated → \0.
21.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "GAT";
char str2[10] = "EG";
strcat(str1, str2);
printf("%c", str1[3]);
}
A) G
B) E
C) T
D) Compiler error
✅ Answer: B
Explanation:
str1 becomes “GATEG” → str1[3] = ‘E’.
22.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
printf("%lu", sizeof(str));
}
A) 4
B) 5
C) 6
D) 0
✅ Answer: B
Explanation:
sizeof(str) includes null character → 5 bytes.
23.
Which function copies string with limit on characters?
A) strcpy()
B) strncpy()
C) strcat()
D) strncat()
✅ Answer: B
Explanation:strncpy(dest, src, n) → copies at most n characters.
24.
Output:
#include <stdio.h>
int main() {
char str[] = "GATE";
str[0] = 'C';
printf("%s", str);
}
A) CATE
B) GATE
C) CATEG
D) Compiler error
✅ Answer: A
Explanation:
First character replaced → “CATE”.
25.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[] = "DEF";
strcpy(str1, str2);
printf("%s", str1);
}
A) ABC
B) DEF
C) ABDEF
D) Compiler error
✅ Answer: B
Explanation:
str2 copied to str1 → str1 = “DEF”.
26.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "ABC";
char str2[] = "ABCD";
printf("%d", strcmp(str1, str2));
}
A) 0
B) Negative number
C) Positive number
D) Compiler error
✅ Answer: B
Explanation:
str1 < str2 → strcmp returns negative.
27.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "CABC";
printf("%c", str[strlen(str)]);
}
A) C
B) Null character (\0)
C) A
D) Compiler error
✅ Answer: B
Explanation:
str[strlen(str)] → null character.
28.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[10] = "XYZ";
strncat(str1, str2, 2);
printf("%s", str1);
}
A) ABC
B) ABCXY
C) ABCXYZ
D) Compiler error
✅ Answer: B
Explanation:strncat() appends only first 2 characters → ABC + XY → ABCXY.
29.
Output:
#include <stdio.h>
int main() {
char str[] = "GATE";
printf("%c", *(str+1));
}
A) G
B) A
C) T
D) E
✅ Answer: B
Explanation:
Pointer arithmetic → *(str+1) → second character → ‘A’.
30.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "C";
char str2[] = "ABC";
strcpy(str1, str2);
printf("%lu", strlen(str1));
}
A) 1
B) 3
C) 4
D) Compiler error
✅ Answer: B
Explanation:
str1 copied → str1 = “ABC” → length = 3.
31.
Which statement is true about strings in C?
A) Strings are null-terminated
B) Strings can contain integers directly
C) Strings cannot be passed to functions
D) Strings cannot be modified
✅ Answer: A
Explanation:
All C strings end with \0.
32.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "ABC";
char str2[] = "abc";
printf("%d", strcmp(str1, str2));
}
A) 0
B) Negative number
C) Positive number
D) Compiler error
✅ Answer: B
Explanation:
Uppercase ‘A’ < lowercase ‘a’ → strcmp returns negative.
33.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "GATE";
char str2[10];
strcpy(str2, str1);
str2[0] = 'C';
printf("%s %s", str1, str2);
}
A) GATE GATE
B) CATE CATE
C) GATE CATE
D) Compiler error
✅ Answer: C
Explanation:
str1 unchanged → “GATE”, str2 modified → “CATE”.
34.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
printf("%d", sizeof(str));
}
A) 4
B) 5
C) 6
D) Compiler error
✅ Answer: B
Explanation:
sizeof includes null → 5 bytes.
35.
Which function compares strings ignoring case?
A) strcmp()
B) strcmpi() / strcasecmp()
C) strncat()
D) strncpy()
✅ Answer: B
Explanation:strcasecmp() compares strings ignoring case (POSIX). Some compilers use strcmpi().
36.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[] = "DEF";
strcat(str1, str2);
printf("%s", str1);
}
A) ABC
B) DEF
C) ABCDEF
D) Compiler error
✅ Answer: C
Explanation:strcat() appends str2 → str1 = “ABCDEF”.
37.
Output:
#include <stdio.h>
int main() {
char str[] = "CABC";
printf("%c", str[0]);
}
A) C
B) A
C) B
D) Compiler error
✅ Answer: A
Explanation:
str[0] → first character → ‘C’.
38.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[10] = "ABC";
printf("%d", strcmp(str1, str2));
}
A) 0
B) 1
C) -1
D) Compiler error
✅ Answer: A
Explanation:
Strings are equal → strcmp returns 0.
39.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
str[4] = 'X';
printf("%s", str);
}
A) GATEX
B) GATE
C) Compiler error
D) GAEX
✅ Answer: A
Explanation:
str[4] originally null → overwritten with ‘X’ → prints “GATEX” (undefined behavior if null is not at end).
40.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "GAT";
char str2[] = "EG";
strncat(str1, str2, 1);
printf("%s", str1);
}
A) GAT
B) GATE
C) GATEG
D) GATE
✅ Answer: B
Explanation:
strncat appends first character of str2 → GAT + E → GATE.
41.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "CABC";
printf("%c", *(str+2));
}
A) C
B) A
C) B
D) Compiler error
✅ Answer: C
Explanation:
Pointer arithmetic → *(str+2) → third character → ‘B’.
42.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[] = "DEF";
strncat(str1, str2, 2);
printf("%s", str1);
}
A) ABC
B) ABCDE
C) ABCDEF
D) Compiler error
✅ Answer: B
Explanation:
strncat appends only first 2 characters → “ABCD” → “ABCDE” (careful: ABC + DE = ABCDE).
43.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "GATE";
char str2[10];
strcpy(str2, str1);
str2[0] = 'C';
printf("%s", str2);
}
A) GATE
B) CATE
C) Compiler error
D) CATG
✅ Answer: B
Explanation:
str2 modified → first character replaced → “CATE”.
44.
Which function returns pointer to first occurrence of character in a string?
A) strchr()
B) strstr()
C) strcpy()
D) strcat()
✅ Answer: A
Explanation:strchr(str, ch) → returns pointer to first occurrence of ch.
45.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "GATE";
printf("%c", str[strlen(str)-1]);
}
A) G
B) A
C) T
D) E
✅ Answer: D
Explanation:
strlen(str) = 4 → last index = 3 → str[3] = ‘E’.
46.
Output:
#include <stdio.h>
int main() {
char str[] = "GATE";
printf("%lu", sizeof(str));
}
A) 4
B) 5
C) 6
D) Compiler error
✅ Answer: B
Explanation:
sizeof includes null → 5 bytes.
47.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "ABC";
char str2[] = "abc";
printf("%d", strcmp(str1, str2));
}
A) 0
B) Negative number
C) Positive number
D) Compiler error
✅ Answer: B
Explanation:
Uppercase ‘A’ < lowercase ‘a’ → strcmp returns negative.
48.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[] = "DEF";
strcat(str1, str2);
printf("%s", str1);
}
A) ABC
B) DEF
C) ABCDEF
D) Compiler error
✅ Answer: C
Explanation:
strcat appends str2 → str1 = “ABCDEF”.
49.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "CABC";
printf("%d", strlen(str));
}
A) 3
B) 4
C) 5
D) Compiler error
✅ Answer: B
Explanation:
strlen excludes null → “CABC” → 4.
50.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "ABC";
char str2[] = "ABC";
printf("%d", strcmp(str1, str2));
}
A) 0
B) 1
C) -1
D) Compiler error
✅ Answer: A
Explanation:
Strings equal → strcmp returns 0.
51.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
str[4] = 'X';
printf("%s", str);
}
A) GATEX
B) GATE
C) Compiler error
D) GAEX
✅ Answer: A
Explanation:
Overwriting null → prints “GATEX” (undefined behavior).
52.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "GAT";
char str2[] = "EG";
strncat(str1, str2, 1);
printf("%s", str1);
}
A) GAT
B) GATE
C) GATEG
D) GATE
✅ Answer: B
Explanation:
Appends first character of str2 → GAT + E → GATE.
53.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "CABC";
printf("%c", str[0]);
}
A) C
B) A
C) B
D) Compiler error
✅ Answer: A
Explanation:
str[0] → first character → ‘C’.
54.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[10] = "XYZ";
printf("%d", strcmp(str1, str2));
}
A) 0
B) Negative number
C) Positive number
D) Compiler error
✅ Answer: B
Explanation:
ABC < XYZ → strcmp returns negative.
55.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
str[0] = 'C';
printf("%s", str);
}
A) CATE
B) GATE
C) CATG
D) Compiler error
✅ Answer: A
Explanation:
First character replaced → “CATE”.
56.
Which function searches substring in a string?
A) strstr()
B) strchr()
C) strcmp()
D) strcpy()
✅ Answer: A
Explanation:strstr(haystack, needle) → returns pointer to first occurrence.
57.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "GATE";
char str2[10];
strcpy(str2, str1);
printf("%s", str2);
}
A) GATE
B) CATG
C) Compiler error
D) Garbage
✅ Answer: A
Explanation:
str1 copied to str2 → str2 = “GATE”.
58.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
printf("%c", *(str+3));
}
A) G
B) A
C) T
D) E
✅ Answer: D
Explanation:
Pointer arithmetic → *(str+3) → last character → ‘E’.
59.
Which of the following replaces first n characters in string?
A) strncpy()
B) strncat()
C) strcpy()
D) strcat()
✅ Answer: A
Explanation:strncpy(dest, src, n) copies n characters → can overwrite first n.
60.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[] = "DEF";
strncat(str1, str2, 2);
printf("%s", str1);
}
A) ABC
B) ABCDE
C) ABCDEF
D) Compiler error
✅ Answer: B
Explanation:
strncat appends first 2 chars → ABC + DE → ABCDE.
61.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "HELLO";
printf("%c", str[strlen(str)]);
}
A) H
B) Null character
C) O
D) Compiler error
✅ Answer: B
Explanation:
Index = strlen → null character \0.
62.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[] = "abc";
printf("%d", strcasecmp(str1, str2));
}
A) 0
B) Negative
C) Positive
D) Compiler error
✅ Answer: A
Explanation:strcasecmp() ignores case → ABC = abc → 0.
63.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[10] = "XYZ";
printf("%d", strcmp(str2, str1));
}
A) Positive
B) Negative
C) 0
D) Compiler error
✅ Answer: A
Explanation:
XYZ > ABC → positive return.
64.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "HELLO";
char str2[10];
strncpy(str2, str1, 3);
str2[3] = '\0';
printf("%s", str2);
}
A) HELLO
B) HEL
C) HELL
D) Compiler error
✅ Answer: B
Explanation:
First 3 chars copied → “HEL”.
65.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
printf("%c", str[2]);
}
A) G
B) A
C) T
D) E
✅ Answer: C
Explanation:
str[2] → third character → ‘T’.
66.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "CABC";
char str2[10];
strcpy(str2, str1);
str2[0] = 'X';
printf("%s", str2);
}
A) CABC
B) XABC
C) Compiler error
D) XXBC
✅ Answer: B
Explanation:
First char replaced → XABC.
67.
Which function returns pointer to last occurrence of character?
A) strchr()
B) strrchr()
C) strstr()
D) strcpy()
✅ Answer: B
Explanation:strrchr() → pointer to last occurrence.
68.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "HELLO";
char str2[] = "LO";
char *p = strstr(str1, str2);
printf("%s", p);
}
A) HELLO
B) LO
C) LLO
D) Compiler error
✅ Answer: C
Explanation:
strstr returns pointer to first occurrence → “LO” in “HELLO” → “LO” from position → prints “LO”.
69.
Output:
#include <stdio.h>
int main() {
char str[6] = "GATE";
printf("%lu", sizeof(str));
}
A) 4
B) 5
C) 6
D) Compiler error
✅ Answer: C
Explanation:
Size of array → 6 bytes allocated.
70.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "HELLO";
printf("%d", strlen(str));
}
A) 5
B) 6
C) 4
D) Compiler error
✅ Answer: A
Explanation:
strlen counts characters excluding null → 5.
71.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[10] = "DEF";
strncat(str1, str2, 1);
printf("%s", str1);
}
A) ABC
B) ABCD
C) ABCDEF
D) Compiler error
✅ Answer: B
Explanation:
strncat → appends first char → ABC + D → ABCD.
72.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[] = "XYZ";
printf("%d", strcmp(str1, str2));
}
A) 0
B) Negative
C) Positive
D) Compiler error
✅ Answer: B
Explanation:
ABC < XYZ → negative return.
73.
Output:
#include <stdio.h>
int main() {
char str[] = "GATE";
str[4] = 'X';
printf("%s", str);
}
A) GATEX
B) GATE
C) Compiler error
D) GAEX
✅ Answer: A
Explanation:
Overwriting null → prints “GATEX”.
74.
Which function compares first n characters?
A) strncmp()
B) strncpy()
C) strncat()
D) strcmp()
✅ Answer: A
Explanation:
`strncmp(str1, str2,
75.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "HELLO";
char str2[] = "HE";
printf("%d", strncmp(str1, str2, 2));
}
A) 0
B) Positive
C) Negative
D) Compiler error
✅ Answer: A
Explanation:strncmp() compares first 2 characters → “HE” = “HE” → returns 0.
76.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
printf("%c", str[1]);
}
A) G
B) A
C) T
D) E
✅ Answer: B
Explanation:
str[1] → second character → ‘A’.
77.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "CABC";
char str2[10];
strcpy(str2, str1);
str2[2] = 'X';
printf("%s", str2);
}
A) CAXC
B) CABC
C) Compiler error
D) CXBC
✅ Answer: A
Explanation:
str2 modified → str2[2] = ‘X’ → “CAXC”.
78.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "HELLO";
printf("%c", *(str+4));
}
A) H
B) O
C) L
D) Compiler error
✅ Answer: B
Explanation:
Pointer arithmetic → *(str+4) → fifth character → ‘O’.
79.
Which function appends n characters from source to destination?
A) strncat()
B) strncpy()
C) strcat()
D) strcpy()
✅ Answer: A
Explanation:strncat(dest, src, n) → appends first n characters.
80.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "HELLO";
printf("%lu", sizeof(str));
}
A) 4
B) 5
C) 10
D) Compiler error
✅ Answer: C
Explanation:
str declared as char[10] → sizeof(str) = 10 bytes.
81.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[10] = "DEF";
strcat(str1, str2);
printf("%s", str1);
}
A) ABC
B) DEF
C) ABCDEF
D) Compiler error
✅ Answer: C
Explanation:
strcat appends str2 → str1 = “ABCDEF”.
82.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
printf("%c", str[strlen(str)-2]);
}
A) G
B) A
C) T
D) E
✅ Answer: C
Explanation:
strlen = 4 → index -2 → str[2] = ‘T’.
83.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "HELLO";
char str2[10];
strncpy(str2, str1, 4);
str2[4] = '\0';
printf("%s", str2);
}
A) HELLO
B) HELL
C) HEL
D) Compiler error
✅ Answer: B
Explanation:
Copies first 4 chars → “HELL”.
84.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "CABC";
str[3] = '\0';
printf("%s", str);
}
A) CABC
B) CAB
C) Compiler error
D) CA
✅ Answer: B
Explanation:
str[3] = ‘\0’ → string truncated → “CAB”.
85.
Which function finds last occurrence of a character?
A) strchr()
B) strrchr()
C) strstr()
D) strcpy()
✅ Answer: B
Explanation:strrchr() → pointer to last occurrence.
86.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[10] = "HELLO";
printf("%s", strchr(str, 'L'));
}
A) LLO
B) HELLO
C) LL
D) Compiler error
✅ Answer: A
Explanation:strchr() returns pointer to first occurrence → prints “LLO”.
87.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "GATE";
char str2[] = "AT";
char *p = strstr(str1, str2);
printf("%s", p);
}
A) GATE
B) AT
C) ATE
D) Compiler error
✅ Answer: C
Explanation:strstr() returns pointer to substring → “ATE”.
88.
Output:
#include <stdio.h>
int main() {
char str[6] = "GATE";
printf("%lu", sizeof(str));
}
A) 4
B) 5
C) 6
D) Compiler error
✅ Answer: C
Explanation:
Array size = 6 → sizeof(str) = 6.
89.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[] = "ABC";
printf("%d", strcmp(str1, str2));
}
A) 0
B) Negative
C) Positive
D) Compiler error
✅ Answer: A
Explanation:
Strings equal → 0.
90.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
printf("%c", str[0]);
}
A) G
B) A
C) T
D) E
✅ Answer: A
Explanation:
First character → ‘G’.
91.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "HELLO";
char str2[10];
strcpy(str2, str1);
str2[1] = 'A';
printf("%s", str2);
}
A) HELLO
B) HALLO
C) HLLLO
D) Compiler error
✅ Answer: B
Explanation:
str2 modified → second character replaced → “HALLO”.
92.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "ABC";
char str2[] = "XYZ";
printf("%d", strcmp(str2, str1));
}
A) Positive
B) Negative
C) 0
D) Compiler error
✅ Answer: A
Explanation:
XYZ > ABC → positive return.
93.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "HELLO";
char str2[10];
strncpy(str2, str1, 3);
str2[3] = '\0';
printf("%s", str2);
}
A) HELLO
B) HEL
C) HELL
D) Compiler error
✅ Answer: B
Explanation:
Copies first 3 characters → “HEL”.
94.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "GATE";
printf("%c", str[strlen(str)-1]);
}
A) G
B) A
C) T
D) E
✅ Answer: D
Explanation:
Last character → ‘E’.
95.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "HELLO";
char str2[] = "HE";
printf("%d", strncmp(str1, str2, 2));
}
A) 0
B) Negative
C) Positive
D) Compiler error
✅ Answer: A
Explanation:
First 2 characters equal → returns 0.
96.
Which function concatenates two strings safely with n characters?
A) strcat()
B) strncat()
C) strncpy()
D) strcpy()
✅ Answer: B
Explanation:strncat() appends at most n characters → safer than strcat.
97.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "HELLO";
char str2[10] = "LO";
printf("%s", strstr(str1, str2));
}
A) LO
B) HELLO
C) LLO
D) Compiler error
✅ Answer: C
Explanation:
strstr → pointer to first occurrence → “LO” → prints “LO” from position → “LO”.
98.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "CABC";
str[1] = '\0';
printf("%s", str);
}
A) CABC
B) C
C) AB
D) Compiler error
✅ Answer: B
Explanation:
str[1] = ‘\0’ → string truncated → “C”.
99.
Output:
#include <stdio.h>
int main() {
char str[6] = "GATE";
printf("%lu", sizeof(str));
}
A) 4
B) 5
C) 6
D) Compiler error
✅ Answer: C
Explanation:
Array of size 6 → sizeof(str) = 6.
100.
Output:
#include <stdio.h>
#include <string.h>
int main() {
char str1[10] = "HELLO";
char str2[] = "EL";
char *p = strstr(str1, str2);
printf("%s", p);
}
A) ELLO
B) HELLO
C) LO
D) Compiler error
✅ Answer: A
Explanation:
strstr → pointer to first occurrence → prints substring starting from “EL” → “ELLO”.