1. How many types of representation are in the string?
A. 1
B. 2
C. 3
D. 4
Answer: B
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>
Answer: C
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
Answer: C
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
#include
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
Answer: D
Explanation:
In the program, We are concatenating the str1 and str2 and printing
it’s total length. So the length is 10.
5. What will be the output of the following C++ code?
#include
#include
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
Answer: C
Explanation:
‘rbegin’ is used to reverse the given the string.
6. What will be the output of the following C++ code?
#include
#include
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
Answer: D
Explanation:
rfind is used to find the characters in the string and replace is used to replace with certain characters.
7. What will be the output of the following C++ code?
#include
#include
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
Answer: B
Explanation:
In this program, We are leaving the first 5 characters and last 7 characters and we are erasing the remaining the characters.
8. What will be the output of the following C++ code?
#include
#include
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
Answer: D
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
#include
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
9223372036854775807
Answer: D
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.
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+=
Answer: D
Explanation:
C++ allows to append more characters to string using both inbuilt append() function and using operator overloaded += operator.
- 10+ TOP C++ MCQs on OOPs Concept – 1
- 10+ TOP C++ MCQs on OOPs Concept – 2
- 10+ TOP C++ MCQs on OOPs Concept – 3
- 10+ TOP C++ MCQs on Character Types
- 10+ TOP C++ MCQs on Integer Types
- 10+ TOP C++ MCQs on Constants
- 10+ TOP C++ MCQs on References – 1
- 10+ TOP C++ MCQs on References – 2