Strings MCQs in C Language
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”.
