String handling functions (strlen, strcmp, etc.) — C++ MCQs

Q#1: Which header file is required for string handling functions like strlen and strcpy?
(A)
(B)
(C)
(D)
Answer: (B)

Q#2: What does strlen(“Hello”) return?
(A) 5
(B) 6
(C) 0
(D) Undefined
Answer: (A) 5

Q#3: Which function copies one string to another?
(A) strcat(dest, src)
(B) strcpy(dest, src)
(C) strcmp(str1, str2)
(D) strlen(str)
Answer: (B) strcpy(dest, src)

Q#4: What is the output of the following?
char str1[10] = “Hi”;
char str2[10];
strcpy(str2, str1);
cout << str2;
(A) Hi
(B) str1
(C) Garbage
(D) Error
Answer: (A) Hi

Q#5: Which function concatenates two strings?
(A) strcat(str1, str2)
(B) strcpy(str1, str2)
(C) strncat(str1, str2)
(D) strcmp(str1, str2)
Answer: (A) strcat(str1, str2)

Q#6: What is returned by strcmp(“abc”, “abc”)?
(A) 1
(B) 1
(C) 0
(D) Depends on compiler
Answer: (C) 0

Q#7: What does strcmp(“abc”, “abd”) return?
(A) 0
(B) Negative value
(C) Positive value
(D) Error
Answer: (B) Negative value

Q#8: Which function compares only the first n characters of two strings?
(A) strncmp(str1, str2, n)
(B) strcmp(str1, str2)
(C) strcopy(str1, str2, n)
(D) strncopy(str1, str2, n)
Answer: (A) strncmp(str1, str2, n)

Q#9: Which function appends only the first n characters of one string to another?
(A) strcat(str1, str2)
(B) strncat(str1, str2, n)
(C) strcpy(str1, str2)
(D) strncpy(str1, str2, n)
Answer: (B) strncat(str1, str2, n)

Q#10: What is the output?
char str[10] = “Hello”;
cout << strlen(str);
(A) 5
(B) 6
(C) Undefined
(D) 4
Answer: (A) 5

Q#11: Which function copies at most n characters from source to destination?
(A) strcpy(dest, src)
(B) strncpy(dest, src, n)
(C) strncat(dest, src, n)
(D) strcat(dest, src)
Answer: (B) strncpy(dest, src, n)

Q#12: What happens if strcpy is used with overlapping source and destination?
(A) Works correctly
(B) Undefined behavior
(C) Copies only part of string
(D) Returns error code
Answer: (B) Undefined behavior

Q#13: Which function returns the length of the string including ‘\0’?
(A) strlen(str)
(B) sizeof(str) (for arrays)
(C) strlength(str)
(D) None
Answer: (B) sizeof(str) (for arrays)

Q#14: What is the output?
char str1[10] = “Hello”;
char str2[10] = “World”;
strcat(str1, str2);
cout << str1;
(A) HelloWorld
(B) Hello
(C) World
(D) Error
Answer: (A) HelloWorld

Q#15: Which function returns 0 if strings are equal, negative if first is less, positive if first is greater?
(A) strcmp
(B) strcpy
(C) strcat
(D) strlen
Answer: (A) strcmp

Q#16: What is the output?
char str1[10] = “abc”;
char str2[10] = “abd”;
cout << strcmp(str1, str2);
(A) Negative value
(B) 0
(C) Positive value
(D) Error
Answer: (A) Negative value

Q#17: Which function concatenates strings safely with specified number of characters?
(A) strncat(str1, str2, n)
(B) strcat(str1, str2)
(C) strcpy(str1, str2)
(D) strncpy(str1, str2, n)
Answer: (A) strncat(str1, str2, n)

Q#18: What is the output?
char str1[10];
strcpy(str1, “Hi”);
cout << str1[2];
(A) \0
(B) i
(C) Garbage
(D) H
Answer: (Not found)

Q#19: Which function does NOT append a null character automatically?
(A) strncpy
(B) strcpy
(C) strcat
(D) strncat
Answer: (A) strncpy

Q#20: Which function returns pointer to first occurrence of a character in a string?
(A) strchr(str, char)
(B) strrchr(str, char)
(C) strstr(str1, str2)
(D) strfind(str, char)
Answer: (A) strchr(str, char)

Q#21: Which function returns pointer to last occurrence of a character?
(A) strchr
(B) strrchr
(C) strstr
(D) strfind
Answer: (B) strrchr

Q#22: Which function searches for a substring in a string?
(A) strstr(str1, str2)
(B) strchr(str1, str2)
(C) strrchr(str1, str2)
(D) strfind(str1, str2)
Answer: (A) strstr(str1, str2)

Q#23: Which function returns non-zero if two strings are not equal?
(A) strcmp
(B) strcpy
(C) strcat
(D) strlen
Answer: (A) strcmp

Q#24: What happens if strcat exceeds the destination array size?
(A) Compiler error
(B) Runtime error
(C) Undefined behavior
(D) Truncates automatically
Answer: (C) Undefined behavior

Q#25: Which function is used to set all characters of a string to a specific character?
(A) memset(str, ch, n)
(B) strcpy
(C) strcat
(D) strset
Answer: (A) memset(str, ch, n)

Q#26: Which function is safest for copying string to avoid buffer overflow?
(A) strcpy
(B) strncpy
(C) strcat
(D) strncat
Answer: (B) strncpy

Q#27: Which function concatenates two strings and limits total characters?
(A) strncat
(B) strncpy
(C) strcat
(D) strcpy
Answer: (A) strncat

Q#28: Which function compares first n characters only?
(A) strncmp
(B) strcmp
(C) strncat
(D) strcopy
Answer: (A) strncmp

Q#29: What is the output?
char str1[10] = “abc”;
char str2[10] = “abc”;
cout << strcmp(str1, str2);
(A) 0
(B) Negative
(C) Positive
(D) Garbage
Answer: (A) 0

Q#30: Which function returns pointer to substring in string?
(A) strstr
(B) strchr
(C) strrchr
(D) strfind
Answer: (A) strstr

Contents Copyrights Reserved By T4Tutorials