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
Explanation: char* are terminated by a ‘\0’ character so the string “Hello\0World” will be cut down to “Hello”.
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
Explanation: 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 <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
Explanation: 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
View Answer
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
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
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
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
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.
- Practice Computer Science MCQs
- Check Programming Books
- Apply for C++ Internship
- Practice Programming MCQs
- Check Computer Science Books