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
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
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
Explanation: 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?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("I like to code in C");
unsigned sz = str.size();
str.resize (sz + 2, '+');
str.resize (14);
cout << str << '\n';
return 0;
}
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
Explanation: 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?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Steve jobs");
cout << str.capacity() << "\n";
return 0;
}
a) 9
b) 10
c) 11
d) Not Fix
View Answer
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:
$ g++ basicst1.cpp $ a.out 10
6. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Steve jobs founded the apple");
string str2 ("apple");
unsigned found = str.find(str2);
if (found != string :: npos)
cout << found << '\n';
return 0;
}
a) apple
b) 12
c) 23
d) Steve jobs founded the
View Answer
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?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Steve jobs");
unsigned long int found = str.find_first_of("aeiou");
while (found != string :: npos)
{
str[found] = '*';
found = str.find_first_of("aeiou", found + 1);
}
cout << str << '\n';
return 0;
}
a) Steve
b) jobs
c) St*v* j*bs
d) St*v*
View Answer
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?
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main ()
{
string str ("Steve jobs");
char * cstr = new char [str.length() + 1];
strcpy (cstr, str.c_str());
char * p = strtok (cstr," ");
while (p != 0)
{
cout << p << '\n';
p = strtok(NULL," ");
}
delete[] cstr;
return 0;
}
a) Steve jo
b) Steve jobs
c)
Steve jobs
d) Steve jo
View Answer
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
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
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.
- Practice Computer Science MCQs
- Practice Programming MCQs
- Check Computer Science Books
- Apply for Information Technology Internship
- Apply for Computer Science Internship