250+ TOP MCQs on String Operations and Answers

C programming questions on “String Operations”. One shall practice these questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C programming questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C programming questions on “String Operations” along with answers, explanations and/or solutions:

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello, world";
  5.         char *str1 = "hello, world";
  6.         if (strcmp(str, str1))
  7.             printf("equal");
  8.         else
  9.             printf("unequal");
  10.     }

a) equal
b) unequal
c) Compilation error
d) Depends on the compiler
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello, world";
  5.         char str1[15] = "hello wo 9";
  6.         strcpy(str, str1);
  7.         printf("%s", str1);
  8.     }

a) Compilation error
b) Segmentation Fault
c) hello, world
d) hello, wo 9
Answer: b
Clarification: None.

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

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         char *str = "hello, world";
  6.         char str1[9];
  7.         strncpy(str1, str, 9);
  8.         printf("%s %d", str1, strlen(str1));
  9.     }

a) hello, world 11
b) hello, wor 9
c) Undefined behaviour
d) Compilation error
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello, worldn";
  5.         printf("%d", strlen(str));
  6.  
  7.     }

a) Compilation error
b) Undefined behaviour
c) 13
d) 11
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char str[11] = "hello";
  5.         char *str1 = "world";
  6.         strcat(str, str1);
  7.         printf("%s %d", str, str[10]);
  8.     }

a) helloworld 0
b) helloworld anyvalue
c) worldhello 0
d) Segmentation fault/code crash
Answer: a
Clarification: None.

6. Strcat() function adds null character.
a) Only if there is space
b) Always
c) Depends on the standard
d) Depends on the compiler
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char str[10] = "hello";
  5.         char *str1 = "world";
  6.         strncat(str, str1, 9);
  7.         printf("%s", str);
  8.     }

a) helloworld
b) Undefined behaviour
c) helloworl
d) hellowor
Answer: a
Clarification: None.

Leave a Reply

Your email address will not be published. Required fields are marked *