C Programming Questions and Answers – String Operations
C Programming Questions and Answers – String Operations
1.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:b
2.What is the output of this C code?
#include <stdio.h>
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
View Answer
Answer:d
3.What is the output of this C code?
#include <stdio.h>
#include <string.h>
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
View Answer
Answer:c
4.What is the output of this C code?
#include <stdio.h>
int main()
{
char *str = “hello, world\n”;
printf(“%d”, strlen(str));
}
a) Compilation error
b) Undefined behaviour
c) 13
d) 11
View Answer
Answer:c
5.What is the output of this C code?
#include <stdio.h>
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
View Answer
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
View Answer
Answer:b
7.What is the output of this C code?
#include <stdio.h>
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
View Answer
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
View Answer
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
View Answer
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);
View Answer
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)
View Answer
Answer:c
12.What will be the value of var for the following?
var = strcmp(“Hello”, “World”);
a) -1
b) 0
c) 1
d) strcmp has void return-type
View Answer
Answer:a
13.What is the output of this C code?
#include <stdio.h>
int main()
{
char str[10] = “hello”;
char *p = strrchr(str, ‘l’);
printf(“%c\n”, *(++p));
}
a) l
b) o
c) e
d) Compilation error
View Answer
Answer:b