C Programming Multiple Choice Questions and Answers on String Operations for Freshers

https://www.computersprofessor.com/2018/01/c-programming-multiple-choice-questions_7.html
1. What is the output of this 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
b) unequal
c) Compilation error
d) Depends on the compiler
Answer: b
2. What is the output of this C code?
#include
int main()
{
char *str = "hello";
char str1[5];
strcpy(str, str1);
printf("%s", str1);
}
a) Compilation error
b) Undefined behaviour
c) hello, world
d) hello, wo 9
b) Undefined behaviour
c) hello, world
d) hello, wo 9
Answer: d
3. What is the output of this 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
b) hello, wor 9
c) Undefined behaviour
d) Compilation error
Answer: c
4. What is the output of this C code?
#include
int main()
{
char *str = "hello, world\n";
printf("%d", strlen(str));
}
a) Compilation error
b) Undefined behaviour
c) 13
d) 11
b) Undefined behaviour
c) 13
d) 11
Answer: c
5. What is the output of this 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
b) helloworld anyvalue
c) worldhello 0
d) Segmentation fault/code crash
Answer: a
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
7. What is the output of this 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
b) Undefined behaviour
c) helloworl
d) hellowor
Answer: a
8. The return-type used in String operations are.
a) void only
b) void and (char *) only
c) void and int only
d) void, int and (char *) only
Answer: d
9. String operation such as strcat(s, t), strcmp(s, t), strcpy(s, t) and strlen(s) heavily rely upon.
a) Presence of NULL character
b) Presence of new-line character
c) Presence of any escape sequence
d) None of the mentioned
Answer: a
10. Which pre-defined function returns a pointer to the last occurence of a character in a string?
a) strchr(s, c);
b) strrchr(s, c);
c) strlchr(s, c);
d) strfchr(s, c);
Answer: b
11. Which of the following function compares 2 strings with case-insensitively?
a) strcmp(s, t)
b) strcmpcase(s, t)
c) strcasecmp(s, t)
d) strchr(s, t)
Answer: c
12. What will be the value of var for the following?
var = strcmp(“Hello”, “World”);
var = strcmp(“Hello”, “World”);
a) -1
b) 0
c) 1
d) strcmp has void return-type
Answer: a
13. What is the output of this C code?
#include
int main()
{
char str[10] = "hello";
char *p = strrchr(str, 'l');
printf("%c\n", *(++p));
}
a) l
b) o
c) e
d) Compilation error
b) o
c) e
d) Compilation error
Answer: b