C++ Programming Questions and Answers – String Class

This section on online C++ Multiple Choice Questions focuses on “String Class”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions comes with the detailed explanation of the answers which helps in better understanding of C++ concepts.

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

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>
View Answer

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
View Answer

Answer: c
Explanation: Both will return the number of characters that conform to the string’s content.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <cstring>
  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 << len << endl;
  14.         return 0;
  15.     }

a) 5
b) 55
c) 11
d) 10
View Answer

Answer: d
Explanation: In the program, We are concatenating the str1 and str2 and printing
it’s total length. So the length is 10.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ stri.cpp
$ a.out
10

5. What will be the output of the following C++ code?

advertisement
  1.     #include <iostream>
  2.     #include <string>
  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 << *r;
  10.         return 0;
  11.     }

a) microsoft
b) micro
c) tfosorcim
d) tfos
View Answer

Answer: c
Explanation: ‘rbegin’ is used to reverse the given the string.
Output:

advertisement
$ g++ stri1.cpp
$ a.out
tfosorcim

6. What will be the output of the following C++ code?

  1.    #include <iostream>
  2.    #include <string>
  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 << str << endl;
  13.        return 0;
  14.    }

a) nobody does like this
b) nobody
c) everybody
d) everybody does like this
View Answer

Answer: d
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?

  1.     #include <iostream>
  2.     #include <string>
  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 << str << endl;      
  10.         return 0;
  11.     }

a) jobs is
b) steve legend
c) steve
d) steve jobs is
View Answer

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.
Output:

$ g++ stri3.cpp
$ a.out
steve legend

8. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Microsoft");
  7.         for (size_t i = 0; i < str.length();)
  8.         {
  9.             cout << str.at(i-1);
  10.         }
  11.         return 0;
  12.     }

a) M
b) Microsoft
c) Micro
d) runtime error
View Answer

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?

  1.    #include <iostream>
  2.    #include <string>
  3.    using namespace std;
  4.    int main ()
  5.    {
  6.        string str ("Ubuntu");
  7.        cout << str.capacity();
  8.        cout << str.max_size();
  9.        return 0;
  10.    }

a) 61073741820
b) 51073741820
c) 6 and max size depends on compiler
d)

15
9223372036854775807
View Answer
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.
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

Answer: d
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.