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?
-
#include
-
int main()
-
{
-
char *str = "hello, world";
-
char *str1 = "hello, world";
-
if (strcmp(str, str1))
-
printf("equal");
-
else
-
printf("unequal");
-
}
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?
-
#include
-
int main()
-
{
-
char *str = "hello, world";
-
char str1[15] = "hello wo 9";
-
strcpy(str, str1);
-
printf("%s", str1);
-
}
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?
-
#include
-
#include
-
int main()
-
{
-
char *str = "hello, world";
-
char str1[9];
-
strncpy(str1, str, 9);
-
printf("%s %d", str1, strlen(str1));
-
}
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?
-
#include
-
int main()
-
{
-
char *str = "hello, worldn";
-
printf("%d", strlen(str));
-
-
}
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?
-
#include
-
int main()
-
{
-
char str[11] = "hello";
-
char *str1 = "world";
-
strcat(str, str1);
-
printf("%s %d", str, str[10]);
-
}
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?
-
#include
-
int main()
-
{
-
char str[10] = "hello";
-
char *str1 = "world";
-
strncat(str, str1, 9);
-
printf("%s", str);
-
}
a) helloworld
b) Undefined behaviour
c) helloworl
d) hellowor
Answer: a
Clarification: None.