250+ TOP MCQs on String Characters and Answers

This section on C++ questions and puzzles on “String Characters”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles 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++ questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

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?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str;
  7.         string str2="Steve jobs";
  8.         string str3="He founded apple";
  9.         str.append(str2);
  10.         str.append(str3, 6, 3);
  11.         str.append(str3.begin() + 6, str3.end());
  12.         str.append(5,0x2e);
  13.         cout << str << 'n';
  14.         return 0;
  15.     }

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?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Test string");
  7.         for ( string :: iterator it = str.begin(); it != 5; ++it)
  8.             cout << *it;
  9.         return 0;
  10.     }

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?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string name ("Jobs");
  7.         string family ("Steve");
  8.         name += " Apple ";
  9.         name += family;
  10.         name += 'n';
  11.         cout << name;
  12.         return 0;
  13.     }

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

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 founded the apple";
  7.         string str2 = str.substr (6, 4);
  8.         unsigned pos = str.find("the");
  9.         string str3 = str.substr (pos);
  10.         cout << str2 << ' ' << str3 << 'n';
  11.         return 0;
  12.     }

a) Jobs the apple
b) the apple
c) Steve
d) Jobs
Answer: a
Clarification: 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?

  1.     #include 
  2.     #include 
  3.     using namespace std;
  4.     int main ()
  5.     {
  6.         string str ("Steve jobs");
  7.         cout << str.length();
  8.         return 0;
  9.     }

a) 8
b) 10
c) 12
d) 9
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: c
Clarification: An empty string is a character array with the NULL character in the zeroth index position.

Leave a Reply

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