C Arrays and Strings-1
1. Which function will you choose to join two words?
a) strcpy()
b) strcat()
c) strncon()
d) memcon()
Answer: B
The strcat() function is used for concatenating two strings, appends a copy of the string.
2. Which among the following is Copying function?
a) memcpy()
b) strcopy()
c) memcopy()
d) strxcpy()
Answer: A
The memcpy() function is used to copy n characters from the object.
3. The _______ function appends not more than n characters.
a) strcat()
b) strcon()
c) strncat()
d) memcat()
Answer: C
The strncat() function appends not more than n characters from the array.
4. What will strcmp() function do?
a) compares the first n characters of the object
b) undefined function
c) copies the string
d) compares the string
Answer: D
The strcmp() function compares the string s1 to the string s2.
5. What is a String in C Language?
a) String is a new Data Type in C
b) String is an array of Characters with null character as the last element of array
c) String is an array of Characters with null character as the first element of array
d) String is an array of Integers with 0 as the last element of array
Answer: B
No explaination is given for that question.
6. Choose a correct statement about C String.
char ary[] = "Hello......!";
a) Character array, ary is a string
b) ary has no Null character at the end
c) String size is not mentioned
d) String can not contain special characters
Answer: A
It is a simple way of creating a C String. You can also define it like the below. \0 is mandatory in this version.
char ary[] = {'h','e','l','l','o','\0'};
7. What is the Format specifier used to print a String or Character array in C Printf or Scanf function?
a) %c
b) %C
c) %s
d) %w
Answer: C
No explaination is given for this question.
8. What will be the output of the following piece of code?
int main() {
char ary[] = "Discovery Channel";
printf("%s", ary);
return 0;
}
a) D
b) Discovery Channel
c) Discovery
d) Compiler error
Answer: B
%s prints the while character array in one go.
9. What is the output of C Program with Strings?
int main() {
char str[] = {'g', 'l', 'o', 'b', 'a', 'l'};
printf("%s", str);
return 0;
}
a) g
b) globe
c) globe\0
d) None of the above
Answer: D
Notice that you have not added the last character \0 in the char array. So it is not a string. It can not know the end of string. So it may print string with some garbage values at the end.
10. What's wrong in the following statement, provided k is a variable of type int?
int main() {
char str[] = {'g', 'l', 'o', 'b', 'a', 'l','\0'};
printf("%s", str);
return 0;
}
a) g
b) globe
c) globe\0
d) Compiler error
Answer: B
Adding a NULL or \0 at the end is a correct way of representing a C string. You can simple use char str[]="globy". It is same as above.