C++ Programming Questions and Answers – String Characters

This section on C++ questions and puzzles focuses 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
View Answer

Answer: b
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

Answer: b
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

Answer: a
Explanation: The characters of a string are stored contiguously in the memory.
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <string>
  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…..
View Answer

Answer: d
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?

advertisement
  1.     #include <iostream>
  2.     #include <string>
  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
View Answer

Answer: d
Explanation: In the for loop, We are not allowed to give a numeric value in string, So it is arising an error.
advertisement

6. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     #include <string>
  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
View Answer

Answer: c
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?

  1.     #include <iostream>
  2.     #include <string>
  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
View Answer

Answer: a
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?

  1.     #include <iostream>
  2.     #include <string>
  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
View Answer

Answer: b
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

Answer: c
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

Answer: c
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.