C++ Programming Questions and Answers – Basic String

This section on C++ interview questions and answers focuses on “Basic String”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin 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++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ interview questions on “Basic String” along with answers, explanations and/or solutions:

1. Which header file is used to manipulate the string?
a) iostream
b) iomanip
c) string
d) container
View Answer

Answer: c
Explanation: To use the string class, We have to use #include<string> header file.

2. How many maximum number of parameters does a string constructor can take?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: string(other_string, position, count). It is a type of constructor for the string.

3. Which constant member functions does not modify the string?
a) bool empty()
b) assign
c) append
d) delete
View Answer

Answer: a
Explanation: Because bool empty is a constant member function, So it can’t be modified.
advertisement
advertisement

4. 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 ("I like to code in C");
  7.         unsigned sz = str.size();
  8.         str.resize (sz + 2, '+');
  9.         str.resize (14);
  10.         cout << str << '\n';
  11.         return 0;
  12.     }

a) I like to code in c
b) I like to code
c) I like to code in c++
d) I like to codeI like to code
View Answer

Answer: b
Explanation: In this program, We are resizing the string by adding + and then we are resizing it to 14.
Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
$ g++ basicst.cpp
$ a.out
I like to code

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 ("Steve jobs");
  7.         cout << str.capacity() << "\n";
  8.         return 0;
  9.     }

a) 9
b) 10
c) 11
d) Not Fix
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. So the answer is not fix, depends on the compiler.
Output:

advertisement
$ g++ basicst1.cpp
$ a.out
10

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 ("Steve jobs founded the apple");
  7.         string str2 ("apple");
  8.         unsigned found = str.find(str2);
  9.         if (found != string :: npos)
  10.             cout << found << '\n';
  11.         return 0;
  12.     }

a) apple
b) 12
c) 23
d) Steve jobs founded the
View Answer

Answer: c
Explanation: In this program, We are finding a string by using the find method.
Output:

$ g++ basicst2.cpp
$ a.out
23

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");
  7.         unsigned long int found = str.find_first_of("aeiou");
  8.         while (found != string :: npos)
  9.         {
  10.             str[found] = '*';
  11.             found = str.find_first_of("aeiou", found + 1);
  12.         }
  13.         cout << str << '\n';
  14.         return 0;
  15.     }

a) Steve
b) jobs
c) St*v* j*bs
d) St*v*
View Answer

Answer: c
Explanation: In this program, We are replacing the vowels with a asterisk by using find_first_of method.
Output:

$ g++ basicst3.cpp
$ a.out
St*v* j*bs

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

  1.     #include <iostream>  
  2.     #include <cstring>
  3.     #include <string>
  4.     using namespace std;
  5.     int main () 
  6.     {
  7.         string str ("Steve jobs");
  8.         char * cstr = new char [str.length() + 1];
  9.         strcpy (cstr, str.c_str());
  10.         char * p = strtok (cstr," ");
  11.         while (p != 0)
  12.         {
  13.             cout << p << '\n';
  14.             p = strtok(NULL," ");
  15.         }
  16.         delete[] cstr;
  17.         return 0;
  18.     }

a) Steve jo
b) Steve jobs
c)

   Steve 
   jobs

d) Steve jo
View Answer

Answer: c
Explanation: In this program, We are breaking up the strings into the form of tokens.
Output:

$ g++ basicst4.cpp
$ a.out
Steve 
jobs

9. What is the difference between unsigned int length() and unsigned int size()?
a) Returns a different value
b) They are same
c) Returns a different value but they are same
d) Returns a length
View Answer

Answer: b
Explanation: Both of them will return the length of strings in the same notations.

10. How many parameters can a resize method take?
a) 1
b) 2
c) 1 or 2
d) 2
View Answer

Answer: c
Explanation: There can be one or two parameters in resize method. They are string length and an optional new character to be inserted.

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.