String Class(C++ Programming ) Questions and Answers
Question 1.
Which method do we use to append more than one character at a time?
append
operator+=
data
both a & b
Explanation:-
Answer: Option D. -> both a & b
None.
Question 2.
What is the output of this program?
1. #include 2. #include 3. using namespace std; 4. int main () 5. { 6. string str ("Ubuntu"); 7. cout
61073741820
51073741820
6 and max size depends on compiler
none of the mentioned
Explanation:-
Answer: Option C. -> 6 and max size depends on compiler
In this program, We are printing the capacity and max size of the string. Output: $ g++ stri5.cpp $ a.out 61073741820
Question 3.
What is the output of this program?
1. #include 2. #include 3. using namespace std; 4. int main () 5. { 6. string str ("Microsoft"); 7. for (size_t i = 0; i < str.length();) 8. { 9. cout
M
Microsoft
Micro
runtime error
Explanation:-
Answer: Option D. -> runtime error
This program will terminate because the cout element is out of range.
Question 4.
What is the output of this program?
1. #include 2. #include 3. using namespace std; 4. int main () 5. { 6. string str ("steve jobs is legend"); 7. string::iterator it; 8. str.erase (str.begin()+ 5, str.end()-7); 9. cout
jobs is
steve legend
steve
none of the mentioned
Explanation:-
Answer: Option B. -> steve legend
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
Question 5.
What is the output of this program?
1. #include 2. #include 3. using namespace std; 4. int main () 5. { 6. string str ("nobody does like this"); 7. string key ("nobody"); 8. size_t f; 9. f = str.rfind(key); 10. if (f != string::npos) 11. str.replace (f, key.length(), "everybody"); 12. cout
nobody does like this
nobody
everybody
everybody does like this
Explanation:-
Answer: Option D. -> everybody does like this
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 everbody does like this
Question 6.
What is the output of this program?
1. #include 2. #include 3. using namespace std; 4. int main () 5. { 6. string str ("microsoft"); 7. string::reverse_iterator r; 8. for (r = str.rbegin() ; r < str.rend(); r++ ) 9. cout
microsoft
micro
tfosorcim
tfos
Explanation:-
Answer: Option C. -> tfosorcim
'rbegin' is used to reverse the given the string. Output: $ g++ stri1.cpp $ a.out tfosorcim
Question 7.
What is the output of this program?
1. #include 2. #include 3. using namespace std; 4. int main () 5. { 6. char str1[10] = "Hello"; 7. char str2[10] = "World"; 8. char str3[10]; 9. int len ; 10. strcpy( str3, str1); 11. strcat( str1, str2) 12. len = strlen(str1); 13. cout
5
55
11
10
Explanation:-
Answer: Option D. -> 10
In the program, We are concatanating the str1 and str2 and printing it’s total length. So the length is 10. Output: $ g++ stri.cpp $ a.out 10
Question 8.
Which is used to return the number of characters in the string?
length
size
both a & b
None of the mentioned
Explanation:-
Answer: Option C. -> both a & b
Both will return the number of characters that conform the string's content.
Question 9.
What is the header file for the string class?
a) #include b) #include c) #include d) None of the mentioned
length
size
both a & b
None of the mentioned
Explanation:-
Answer: Option C. -> both a & b
Answer : c Explanation:None.
Question 10.
How many types of representation are in string?
1
2
3
4
Explanation:-
Answer: Option B. -> 2
C++ provides following two types of string representations. They are C-style character string and