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
Answer: b
Clarification: 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) @
Answer: b
Clarification: 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
Answer: a
Clarification: The characters of a string are stored contiguously in the memory.
4. What will be the output of the following C++ code?
-
#include
-
#include
-
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…..
Answer: d
Clarification: 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
-
#include
-
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
Answer: d
Clarification: 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
-
#include
-
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
Answer: c
Clarification: In this program, We are adding the characters at the end of the current value.
Output:
$ g++ str1.cpp $ a.out Jobs Apple Steve