C++ Programming Questions and Answers – String – 2

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “String – 2”.

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

#include <iostream> 
#include <string>
#include <cstring>
using namespace std; 
int main(int argc, char const *argv[])
{
	const char *a = "Hello\0World";
	cout<<a;
	return 0;
}

a) Hello World
b) Hello
c) World
d) Error
View Answer

Answer: b
Explanation: char* are terminated by a ‘\0’ character so the string “Hello\0World” will be cut down to “Hello”.
advertisement

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

#include <iostream> 
#include <string>
#include <cstring>
using namespace std; 
int main(int argc, char const *argv[])
{
	string s("a");
	cout<<s;
	return 0;
}

a) a
b) empty string
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: string class has a constructor for this call hence the string s will be assigned “a”.
Free 30-Day Java Certification Bootcamp is Live. Join Now!

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

#include <iostream> 
#include <string>
#include <cstring>
using namespace std; 
int main()
{
	string s('a');
	cout<<s;
	return 0;
}

a) a
b) empty string
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: The string class provides string(string s) as a constructor not the string(char) as a constructor therefore this assignment is not valid.
advertisement

4. Which is the correct way of concatenating a character at the end of a string object?

way 1:
string s;
s = s + 'a';
 
way 2:
string s;
s.push_back('a');

a) 1 only
b) 2 only
c) both of them
d) both are wrong
View Answer

Answer: c
Explanation: string class provides the addition of char and string and also push_back(char) function to append a character at the end of a string.

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

#include <iostream>
#include <string>
using namespace std;
int main ()
{
  std::string str ("Sanfoundry.");
  str.back() = '!';
  std::cout << str << endl;
  return 0;
}

a) Sanfoundry.!
b) Sanfoundry.
c) Sanfoundry!
d) Sanfoundry!.
View Answer

Answer: c
Explanation: back() function modifies the last character of the string with the character provided.

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

#include <iostream>
#include <string>
using namespace std;
int main ()
{
  string str ("sanfoundry.");
  str.front() = 'S';
  cout << str << endl;
  return 0;
}

a) Sanfoundry
b) Sanfoundry.
c) sanfoundry
d) sanfoundry.
View Answer

Answer: b
Explanation: front() modifies the first character of the string with the character provided.

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

#include <iostream>
#include <string>
using namespace std;
int main ()
{
  string str ("sanfoundry.");
  cout << str.substr(3).substr(4) << endl;
  return 0;
}

a) foundry.
b) dry.
c) oundry.
d) found
View Answer

Answer: b
Explanation: As we are first taking the substring of s from 3 to end then on that substring we are taking substr from 4 to end which is equal to “dry.”.

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

#include <iostream>
#include <string>
using namespace std;
int main ()
{
  string str = "Sanfoundry!";
  cout<<str.capacity();
  cout<<str.size();
  return 0;
}

a) 1511
b) 1111
c) 1115
d) 010
View Answer

Answer: a
Explanation: Capacity of a string object is defined as the length of string plus the extra space given to that object which will be used further if string is expanded.

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.

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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.