This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “String – 1”.
1. What is string objects in C++?
a) Stream of alphabets
b) A stream of well-defined characters
c) Stream of characters
d) A stream of characters terminated by \0
View Answer
Explanation: String is defined as streams of characters, not necessarily terminated by \0. Also, a string can contain characters other than alphabets.
2. What is Character-Array?
a) array of alphabets
b) array of well-defined characters
c) array of characters
d) array of characters terminated by \0
View Answer
Explanation: Character-Array is defined as an array of characters, not necessarily terminated by \0. Also, a character-array can contain characters other than alphabets.
3. Pick the incorrect statement about Character-Array.
a) Character-Array can be terminated by a null character(‘\0’)
b) Character-Array has a static size
c) Character-Array has a dynamic size
d) Character-Array has a threat of array-decay
View Answer
Explanation: As Character-Array is an array, its size should be defined during its declaration hence the size of Character-Array is static. A Character-Array is not necessarily to be terminated by a null character. Also, it has a threat of array-decay.
4. Pick the correct statement about string objects in C++.
a) String objects must be terminated by a null character(‘\0’)
b) String objects have a static size
c) String objects have a dynamic size
d) String objects use extra memory than required.
View Answer
Explanation: String objects are dynamic in nature i.e. their size varies as their value changes so they don’t use any extra memory and it is not necessary to terminate a string object by ‘\0’.
5. What will be the output of the following C++ code?
#include <iostream> #include <string> using namespace std; int main(int argc, char const *argv[]) { string str; cin>>str; cout<<str; return 0; }
a) str
b) Input provided by the user
c) Error
d) Segmentation fault
View Answer
Explanation: There is no error in the program and as we are asking the user to enter a string and printing that string to console. Therefore output will be the string provided by the user.
6. What will be the output of the following C++ code?
#include <iostream> #include <string> using namespace std; int main(int argc, char const *argv[]) { char str[] = "Hello World"; cout<<str[0]; return 0; }
a) H
b) e
c) Error
d) o
View Answer
Explanation: The program has no errors so and as str = “Hello World” and we are trying to print the first character of str. Hence “H” is the answer.
7. What will be the output of the following C++ code?
#include <iostream> #include <string> using namespace std; int main(int argc, char const *argv[]) { char str[10]; cin>>str; cout<<str; return 0; }
a) Compiler-time Error
b) Run-time Error
c) Input given by the user
d) Depends on the length of the string entered by the user
View Answer
Explanation: As the character array size is 10 so if the string entered by the user is <= 10 then there will be no error and the program runs perfectly otherwise if the length is > 10 then the program gives a run-time error because the string crosses the allocated memory space.
Output:
length < 10 $ ./a.out Hello Hello length > 10 $ ./a.out C++Programming *** stack smashing detected ***:terminated Aborted (core dumped)
8. What will be the output of the following C++ code if the string entered by the user is “Hello World”?
#include <iostream> #include <string> using namespace std; int main(int argc, char const *argv[]) { string str; cin>>str; cout<<str; return 0; }
a) Hello World
b) Hello
c) World
d) Error
View Answer
Explanation: As cin considers \n or space as the terminating symbols for the input so when the user enters “Hello World” so only “Hello” will be stored into the str variable as cin stops scanning input after space.
Output:
$ ./a.out Hello World Hello
9. Which header file is used to include the string object functions in C++?
a) #include <string.h>
b) #include <cstring>
c) #include <string>
d) #include <string.cpp>
View Answer
Explanation: #include <string> header file is used as it contains all the string object functions.
10. What will be the output of the following C++ code?
#include <iostream> #include <string> using namespace std; int main(int argc, char const *argv[]) { char s1[6] = "Hello"; char s2[6] = "World"; char s3[12] = s1 + " " + s2; cout<<s3; return 0; }
a) Hello World
b) Hello
c) World
d) Error
View Answer
Explanation: There is no operation defined for the addition of character array in C++ hence the compiler throws an error as it does not understoods what to do about this expression.
11. What will be the output of the following C++ code?
#include <iostream> #include <string> using namespace std; int main(int argc, char const *argv[]) { string s1 = "Hello"; string s2 = "World"; string s3 = s1 + " " + s2; cout<<s3; return 0; }
a) Hello World
b) Hello
c) World
d) Error
View Answer
Explanation: The program runs perfectly as string class has defined the addition of two strings so when two strings are added then both the strings are concatenated. Hence the output is “Hello World”.
12. Which of the following is correct way of concatenating two string objects in C++?
way 1: string s1 = "hello"; string s2 = "world"; string s3 = s1 + s2; way 2: string s1 = "hello"; string s2 = "world"; string s3 = s1.append(s2); way 3: string s1 = "hello"; string s2 = "world"; string s3 = strcat(s1,s2);
a) 1 and 2
b) 2 and 3
c) 1 and 3
d) 1, 2 and 3
View Answer
Explanation: To concatenate two string objects we are provided with either direct addition or append() function in string class but strcat() is char* function hence they cannot be used to concatenate two string objects.
13. Which of the following is not a modifier function in string class?
a) operator+=()
b) operator[]()
c) push_back()
d) erase()
View Answer
Explanation: [] operator is used to access one of the characters of the string objects whereas other functions are used to modify the string in some way.
14. Which function is used to get the length of a string object?
a) str.length()
b) str.size()
c) str.max_size()
d) both size() and length() function
View Answer
Explanation: Both size() and length() are used to get the size of the string objects.
15. What is the identifier given to string class to declare string objects?
a) String
b) string
c) STRING
d) Any of the above can be used
View Answer
Explanation: string identifier is used as the name of the class string.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Practice Programming MCQs
- Check Programming Books
- Check Computer Science Books
- Check C++ Books
- Apply for C++ Internship