Here is a listing of C++ questions and puzzles on “String Characters” along with answers, explanations and/or solutions:
1. Which is an instantiation of the basic_string class template?
a) Character
b) String class
c) Memory
d) Iterator
View Answer
Explanation: The string class is an instantiation of the basic_string class template.
2. Which character is used to terminate the string?
a) $
b) Null
c) Empty
d) @
View Answer
Explanation: A string of characters is stored in successive elements of a character array are terminated by the NULL character.
3. How does the strings are stored in the memory?
a) Contiguous
b) Non-contiguous
c) Null
d) sequence
View Answer
Explanation: The characters of a string are stored contiguously in the memory.
4. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str;
string str2="Steve jobs";
string str3="He founded apple";
str.append(str2);
str.append(str3, 6, 3);
str.append(str3.begin() + 6, str3.end());
str.append(5,0x2e);
cout << str << '\n';
return 0;
}
a) Steve jobs
b) He founded apple
c) Steve
d) Steve jobsndended apple…..
View Answer
Explanation: In this program, We are adding characters to the string, So the output will as follows after addition of characters.
Output:
$ g++ str.cpp $ a.out Steve jobsndended apple.....
5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Test string");
for ( string :: iterator it = str.begin(); it != 5; ++it)
cout << *it;
return 0;
}
a) Test
b) string
c) Test string
d) Error
View Answer
Explanation: In the for loop, We are not allowed to give a numeric value in string, So it is arising an error.
6. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name ("Jobs");
string family ("Steve");
name += " Apple ";
name += family;
name += '\n';
cout << name;
return 0;
}
a) Steve Jobs
b) Apple
c) Jobs Apple Steve
d) Apple Steve
View Answer
Explanation: In this program, We are adding the characters at the end of the current value.
Output:
$ g++ str1.cpp $ a.out Jobs Apple Steve
7. 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 = str.substr (6, 4);
unsigned pos = str.find("the");
string str3 = str.substr (pos);
cout << str2 << ' ' << str3 << '\n';
return 0;
}
a) Jobs the apple
b) the apple
c) Steve
d) Jobs
View Answer
Explanation: In this program, We are finding the substring of the string by using the substr function.
Output:
$ g++ str2.cpp $ a.out Jobs the apple
8. 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.length();
return 0;
}
a) 8
b) 10
c) 12
d) 9
View Answer
Explanation: In this program, We are finding the length of the string.
Output:
$ g++ str3.cpp $ a.out 10
9. Where are the strings stored?
a) Stack
b) Heap
c) Both Stack & Heap
d) Queue
View Answer
Explanation: Dynamic strings(dynamic length) are stored in heap and static string(with fixed size) are stored in stack.
10. What will happen if a string is empty?
a) It can’t be created
b) Raises an error
c) It can be used
d) It cannot be used
View Answer
Explanation: An empty string is a character array with the NULL character in the zeroth index position.
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.
- Apply for Computer Science Internship
- Check Programming Books
- Practice Computer Science MCQs
- Check C++ Books
- Apply for C++ Internship