250+ TOP MCQs on Basic String and Answers

This section on C++ interview questions and answers 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
Answer: c
Clarification: To use the string class, We have to use #include header file.

2. How many maximum number of parameters does a string constructor can take?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: 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
Answer: a
Clarification: Because bool empty is a constant member function, So it can’t be modified.

4. 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 ("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
Answer: b
Clarification: In this program, We are resizing the string by adding + and then we are resizing it to 14.
Output:

$ g++ basicst.cpp
$ a.out
I like to code

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

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

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

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 ("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
Answer: c
Clarification: 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 
  2.     #include 
  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*
Answer: c
Clarification: 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   
  2.     #include 
  3.     #include 
  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
Answer: c
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: There can be one or two parameters in resize method. They are string length and an optional new character to be inserted.

  250+ TOP MCQs on STL Container Any – 1 and Answers

Leave a Comment

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

Scroll to Top