250+ TOP MCQs on String – 2 and Answers

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

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

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

a) Hello World
b) Hello
c) World
d) Error
Answer: b
Clarification: char* are terminated by a ‘ ’ character so the string “Hello World” will be cut down to “Hello”.

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

#include  
#include 
#include 
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
Answer: a
Clarification: string class has a constructor for this call hence the string s will be assigned “a”.

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

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

a) a
b) empty string
c) Error
d) Segmentation fault
Answer: c
Clarification: The string class provides string(string s) as a constructor not the string(char) as a constructor therefore this assignment is not valid.

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
Answer: c
Clarification: 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 
#include 
using namespace std;
int main ()
{
  std::string str (".");
  str.back() = '!';
  std::cout << str << endl;
  return 0;
}

a) .!
b) .
c) !
d) !.
Answer: c
Clarification: 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 
#include 
using namespace std;
int main ()
{
  string str (".");
  str.front() = 'S';
  cout << str << endl;
  return 0;
}

a)
b) .
c)
d) .
Answer: b
Clarification: front() modifies the first character of the string with the character provided.

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

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

a) foundry.
b) dry.
c) oundry.
d) found
Answer: b
Clarification: 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 
#include 
using namespace std;
int main ()
{
  string str = "!";
  cout<<str.capacity();
  cout<<str.size();
  return 0;
}

a) 1511
b) 1111
c) 1115
d) 010
Answer: a
Clarification: 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.

  250+ TOP MCQs on Default Arguments and Answers

Leave a Comment

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

Scroll to Top