250+ TOP MCQs on String Class and Answers

This section on online C++ quiz on “String Class”. One shall practice these online quizzes 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++ quiz comes with the detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of online C++ quiz 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
Answer: b
Clarification: 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
b) #include
c) #include
d) #include
Answer: c
Clarification: #include 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
Clarification: 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?

  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 << len << endl;
  14.         return 0;
  15.     }

a) 5
b) 55
c) 11
d) 10
Answer: d
Clarification: 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?

  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 << *r;
  10.         return 0;
  11.     }

a) microsoft
b) micro
c) tfosorcim
d) tfos
Answer: c
Clarification: ‘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?

  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 << str << endl;
  13.        return 0;
  14.    }

a) nobody does like this
b) nobody
c) everybody
d) everybody does like this
Answer: d
Clarification: 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 
  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 << str << endl;      
  10.         return 0;
  11.     }

a) jobs is
b) steve legend
c) steve
d) steve jobs is
Answer: b
Clarification: 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 
  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 << str.at(i-1);
  10.         }
  11.         return 0;
  12.     }

a) M
b) Microsoft
c) Micro
d) runtime error
Answer: d
Clarification: 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 
  2.    #include 
  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
Clarification: 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+=
Answer: d
Clarification: C++ allows to append more characters to string using both inbuilt append() function and using operator overloaded += operator.

  250+ TOP MCQs on Essential Operators and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top