My Report

C++ String Test – 1


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. 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;
   }

2. 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;
    }

3. What is the header file for the string class?

4. Which method do we use to append more than one character at a time?

5. How many types of representation are in the string?

6. 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;
    }

7. 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;
    } 

8. Which is used to return the number of characters in the string?

9. 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;
    }

10. 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;
   }

 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

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.