Here is a listing of C++ Multiple Choice Questions & Answers focuses on “String Class” along with answers, explanations and/or solutions:
1. How many types of representation are in the string?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: C++ provides the following two types of string representations. They are C-style character string and string class type with Standard C++.
2. What is the header file for the string class?
a) #include<ios>
b) #include<str>
c) #include<string>
d) #include<stio>
View Answer
Explanation: #include<string> is the header file for the string class.
3. Which is used to return the number of characters in the string?
a) length
b) size
c) both size & length
d) name
View Answer
Explanation: Both will return the number of characters that conform to the string’s content.
4. What will be the output of the following C++ code?
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
strcpy( str3, str1);
strcat( str1, str2);
len = strlen(str1);
cout << len << endl;
return 0;
}
a) 5
b) 55
c) 11
d) 10
View Answer
Explanation: In the program, We are concatenating the str1 and str2 and printing
it’s total length. So the length is 10.
Output:
$ g++ stri.cpp $ a.out 10
5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("microsoft");
string::reverse_iterator r;
for (r = str.rbegin() ; r < str.rend(); r++ )
cout << *r;
return 0;
}
a) microsoft
b) micro
c) tfosorcim
d) tfos
View Answer
Explanation: ‘rbegin’ is used to reverse the given the string.
Output:
$ g++ stri1.cpp $ a.out tfosorcim
6. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("nobody does like this");
string key ("nobody");
size_t f;
f = str.rfind(key);
if (f != string::npos)
str.replace (f, key.length(), "everybody");
cout << str << endl;
return 0;
}
a) nobody does like this
b) nobody
c) everybody
d) everybody does like this
View Answer
Explanation: rfind is used to find the characters in the string and replace is used to replace with certain characters.
Output:
$ g++ stri2.cpp $ a.out everybody does like this
7. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("steve jobs is legend");
string::iterator it;
str.erase (str.begin()+ 5, str.end()-7);
cout << str << endl;
return 0;
}
a) jobs is
b) steve legend
c) steve
d) steve jobs is
View Answer
Explanation: In this program, We are leaving the first 5 characters and last 7 characters and we are erasing the remaining the characters.
Output:
$ g++ stri3.cpp $ a.out steve legend
8. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Microsoft");
for (size_t i = 0; i < str.length();)
{
cout << str.at(i-1);
}
return 0;
}
a) M
b) Microsoft
c) Micro
d) runtime error
View Answer
Explanation: This program will terminate because the cout element is out of range.
9. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Ubuntu");
cout << str.capacity();
cout << str.max_size();
return 0;
}
a) 61073741820
b) 51073741820
c) 6 and max size depends on compiler
d)
15 9223372036854775807View Answer
Explanation: str.capacity() returns the size of the storage space currently allocated for the string, expressed in terms of bytes and capacity of the string may be equal or greater. The max_size() returns the max size of the string.
Output:
$ g++ stri5.cpp $ a.out 15 9223372036854775807
10. Which method do we use to append more than one character at a time?
a) append
b) operator+=
c) data
d) both append & operator+=
View Answer
Explanation: C++ allows to append more characters to string using both inbuilt append() function and using operator overloaded += operator.
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.
- Practice Computer Science MCQs
- Practice Programming MCQs
- Check C++ Books
- Check Computer Science Books
- Apply for Computer Science Internship