C Questions and Answers – String Operations – 4

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “String Operations – 4”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. What will be returned in the following C code?

size- t strlen(const char *s)
const char *sc;
for(sc = s; *sc!= ' \ O ' ; ++sc)
return(sc - s) ;

a) number of characters equal in sc
b) length of string s
c) doesn’t return any value
d) displays string s
View Answer

Answer: b
Explanation: The strlen() function is used to return the length of the string s.
advertisement
advertisement

2. The function strsp() is the complement of strcspn().
a) true
b) false
View Answer

Answer: a
Explanation: Both strcspn() and strpbrk() perform the same function. Only strspn() the return values differ. The function strspn() is the complement of strcspn() .
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What will the following C code do?

char * strrchr(const char *s, int c )
char ch = c;
char *sc;
for(sc = NULL; ; ++s)
if(*s == ch)
SC = 9;
i f (*s =='\O' )
return (( char *) s);

a) find last occurrence of c in char s[ ].
b) find first occurrence of c in char s[ ].
c) find the current location of c in char s[ ].
d) There is error in the given code
View Answer

Answer: a
Explanation:The strrchr() function locates the last occurrence of c (converted to a char) in the string pointed to by s. String contains null character as a terminating part of it.
advertisement

4. This function offers the quickest way to determine whether two character sequences of the same known length match character for the character up to and including any null character in both.
a) strcmp()
b) memcmp()
c) strncmp()
d) no such function
View Answer

Answer: c
Explanation: The strncmp() function is used to compare not more than n characters (characters that follow a null character are not compared) from the array pointed to by one, to the array pointed to by other.
advertisement

5. What will be the output of the following C code?

char str1[15];
char str2[15];
int mat;
strcpy(str1, "abcdef");
strcpy(str2, "ABCDEF");
mat= strncmp(str1, str2, 4);
if(mat< 0)
printf("str1 is not greater than str2");
else if(mat> 0)
printf("str2 is is not greater than str1");
else
printf("both are equal");

a) str1 is not greater than str2
b) str2 is not greater than str1
c) both are equal
d) error in given code
View Answer

Answer: b
Explanation: The C library function int strncmp(const char *str1, const char *str2, size_t n) is used to compare at most the first n bytes of str1 and str2.

6. What will be the output of the following C code?

void *memset(void *c, int c, size-t n) 
unsigned char ch = c;
unsigned char *su;
for (su = s; 0 < n; ++su, --n)
<br>
*su = ch;
<br>

a) store c throughout unsigned char s[n]
b) store c throughout signed char s[n]
c) find first occurrence of c in s[n]
d) find last occurrence of c in s[n]
View Answer

Answer: a
Explanation: This is the safe way to store the same value throughout a contiguous sequence of elements in a character array.

7. Use_______to determine the null-terminated message string that corresponds to the error code errcode.
a) strerror()
b) strstr()
c) strxfrm()
d) memset()
View Answer

Answer: a
Explanation: Use strerror (errcode) to determine the null-terminated message string that corresponds to the error code errcode.

8. What will be the output of the following C code?

const char str1[10]="Helloworld";
const char str2[10] = "world";
char *mat;
mat = strstr(str1, str2);
printf("The substring is:%s\n", mat);

a) The substring is:world
b) The substring is:Hello
c) The substring is:Helloworld
d) error in the code
View Answer

Answer: a
Explanation: The C library function char *strstr(const char *str1, const char *str2) is used to find the first occurrence of the substring str2 in the string str1.The terminating ‘\0’ characters are not compared.

9. void *memcpy(void *dest, const void *src, size_t n) What does the following code do?
a) copies n characters from src to dest
b) copies n character from dest to src
c) transform n character from dest to src
d) transform n character from src to dest
View Answer

Answer: a
Explanation: In the C library function memcpy() is used to copy n characters from memory area src to memory area dest.

10. What will the given C code do?

int memcmp(const void *str1, const void *str2, size_t n)

a) compares the first n bytes of str1 and str2
b) copies the first n bytes of str1 to str2
c) copies the first n bytes of str2 to str1
d) invalid function
View Answer

Answer: a
Explanation: In the C library function int memcmp(const void *str1, const void *str2, size_t n)) is used to compare the first n bytes of memory area str1 and memory area str2.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.