C++ Programming Questions and Answers – String Class 5

C++ Multiple Choice Questions focuses on “String Class”. One shall practice these questions to improve their C++ programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. C++ questions comes with the detailed explanation of the answers which helps in better understanding of C++ concepts.

C++ Multiple Choice Questions & Answers focuses on “String Class” along with answers, explanations and/or solutions:

Q 1. How many types of representation are in the string?
A. 1
B. 2
C. 3
D. 4

Show Answer Answer:-B. 2
Explanation C++ provides the following two types of string representations. They are C-style character string and string class type with Standard C++.

Q 2. What is the header file for the string class?
A. #include<ios>
B. #include<str>
C. #include<string>
D. #include<stio>

Show Answer Answer:-C. #include
Explanation #include is the header file for the string class.

Q 3. Which is used to return the number of characters in the string?
A. length
B. size
C. name
D. both size & length

Show Answer Answer:-D. both size & length
Explanation Both will return the number of characters that conform to the string’s content.

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

#include <iostream>

#include <cstring>

using namespace std;

int main ()

{

char str1[10] = “Hello”;

char str2[10] = “World”;

char str3[10];

int len ;

strcpy( str3, str1);

strcat( str1, str2);

len = strlen(str1);

cout << len << endl;

return 0;

}

A. 5
B. 6
C. 8
D. 10

Show Answer Answer:-D. 10
Explanation In the program, We are concatenating the str1 and str2 and printing it’s total length. So the length is 10. Output: $ g++ stri.cpp $ a.out 10

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

A. microsoft
B. micro
C. tfos
D. tfosorcim

Show Answer Answer:-D. tfosorcim
Explanation ‘rbegin’ is used to reverse the given the string. Output: $ g++ stri1.cpp $ a.out tfosorcim

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

#include <iostream>

#include <string>

using namespace std;

int main ()

{

string str (“nobody does like this”);

string key (“nobody”);

size_t f;

f = str.rfind(key);

if (f != string::npos)

str.replace (f, key.length(), “everybody”);

cout << str << endl;

return 0;

}

A. nobody does like this
B. nobody
C. everybody
D. everybody does like this

Show Answer Answer:-D. everybody does like this
Explanation rfind is used to find the characters in the string and replace is used to replace with certain characters. Output: $ g++ stri2.cpp $ a.out everybody does like this

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

#include <iostream>

#include <string>

using namespace std;

int main ()

{

string str (“steve jobs is legend”);

string::iterator it;

str.erase (str.begin()+ 5, str.end()-7);

cout << str << endl;

return 0;

}

A. jobs is
B. steve
C. steve legend
D. steve jobs is

Show Answer Answer:-C. steve legend
Explanation In this program, We are leaving the first 5 characters and last 7 characters and we are erasing the remaining the characters. Output: $ g++ stri3.cpp $ a.out steve legend

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

#include <iostream>

#include <string>

using namespace std;

int main ()

{

string str (“Microsoft”);

for (size_t i = 0; i < str.length();)

{

cout << str.at(i-1);

}

return 0;

}

A. M
B. Microsoft
C. Micro
D. runtime error

Show Answer Answer:-D. runtime error
Explanation This program will terminate because the cout element is out of range.

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

A. 61073741820
B. 51073741820
C. 6 and max size depends on compiler
D. 15

9223372036854775807

Show Answer Answer:-D. 15 9223372036854775807
Explanation str.capacity() returns the size of the storage space currently allocated for the string, expressed in terms of bytes and capacity of the string may be equal or greater. The max_size() returns the max size of the string. Output: $ g++ stri5.cpp $ a.out 15 9223372036854775807

Q 10. Which method do we use to append more than one character at a time?
A. append
B. operator+=
C. data
D. both append & operator+=

Show Answer Answer:-D. both append & operator+=
Explanation C++ allows to append more characters to string using both inbuilt append() function and using operator overloaded += operator.

Comments

Leave a Reply

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

You cannot copy content of this page