C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “String – 1”.
Q 1.What will be the output of the following C++ code?
#include#include using namespace std; int main(int argc, char const *argv[]) { char str[] = "Hello World"; cout< A. HB. eC. oD. Error
Show Answer
Answer:-A. HExplanation
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.Q 2. What will be the output of the following C++ code?
#include#include using namespace std; int main(int argc, char const *argv[]) { char str[10]; cin>>str; cout< A. Compiler-time ErrorB. Run-time ErrorC. Input given by the userD. Depends on the length of the string entered by the user
Show Answer
Answer:-D. Depends on the length of the string entered by the userExplanation
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)Q 3.What will be the output of the following C++ code if the string entered by the user is “Hello World”?
#include#include using namespace std; int main(int argc, char const *argv[]) { string str; cin>>str; cout< A. Hello WorldB. HelloC. WorldD. Error
Show Answer
Answer:-B. HelloExplanation
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 HelloQ 4. Which header file is used to include the string object functions in C++?
Show Answer
Answer:-B. #includeExplanation
#includeQ 5. What will be the output of the following C++ code?
#include#include using namespace std; int main(int argc, char const *argv[]) { char s1[6] = "Hello"; char s2[6] = "World"; char s3[12] = s1 + " " + s2; cout< A. Hello WorldB. HelloC. WorldD. Error
Show Answer
Answer:-D. ErrorExplanation
There is no operation defined for the addition of character array in C++ hence the compiler throws an error as it does not understood what to do about this expression.Q 6. What will be the output of the following C++ code?
#include#include using namespace std; int main(int argc, char const *argv[]) { string s1 = "Hello"; string s2 = "World"; string s3 = s1 + " " + s2; cout< A. Hello WorldB. WorldC. HelloD. Error
Show Answer
Answer:-A. Hello WorldExplanation
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”.Q 7. 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);
Show Answer
Answer:-A. 1 and 2Explanation
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.Q 8.Which of the following is not a modifier function in string class?
Show Answer
Answer:-C. operator[]()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.Q 9. Which function is used to get the length of a string object?
Show Answer
Answer:-D. both size() and length() functionExplanation
Both size() and length() are used to get the size of the string objects.Q 10. What is the identifier given to string class to declare string objects?