C++ Arrays & Strings | Set 2
11. Which header file is used to manipulate the string?
a) iostream
b) iomanip
c) string
d) container
Answer: C
Explanation: To use the string class, We have to use #include<string> header file.
12. How many maximum number of parameters does a string constructor can take?
a) 1
b) 2
c) 3
d) 4
Answer: C
Explanation: string(other_string, position, count). It is a type of constructor for the string.
13. Which constant member functions does not modify the string?
a) bool empty()
b) assign
c) append
d) delete
Answer: A
Explanation: Because bool empty is a constant member function, So it can’t be modified.
14. What is the difference between unsigned int length() and unsigned int size()?
a) Returns a different value
b) They are same
c) Returns a different value but they are same
d) Returns a length
Answer: B
Explanation: Both of them will return the length of strings in the same notations.
15. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main() {
string str {"Steve jobs"};
cout << str.capacity() << "\n";
return 0;
}
a) 9
b) 10
c) 11
d) Not Fix
Answer: D
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. So the answer is not fix, depends on the compiler.
16. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main() {
string str {"Steve jobs founded the apple"};
string str {"apple"};
unsigned found = str.find(str2);
if (found != string :: npos)
cout << found << "\n";
return 0;
}
a) apple
b) 12
c) 23
d) Steve jobs founded the
Answer: C
Explanation: In this program, We are finding a string by using the find method.
17. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
int main() {
string str {"Steve jobs"};
unsigned long int found = str.find_first_of("aeiou");
while (found != string :: npos)
{
str[found] = '*';
found = str.find_first_of("aeiou", found + 1);
}
cout << str << "\n";
return 0;
}
a) Steve
b) jobs
c) St*v* j*bs
d) St*v*
Answer: C
Explanation: In this program, We are replacing the vowels with a asterisk by using find_first_of method.
18. What will be the output of the following C++ code?
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main() {
string str {"Steve jobs"};
char * cstr = new char [str.length() + 1];
strcpy (cstr, str.c_str());
char * p = strtok (cstr, " ");
while (p != 0)
{
cout << p << "\n";
p = strtok(NULL, " ");
}
delete[] cstr;
return 0;
}
a) Steve jo
b) Steve jobs
c) Steve
jobs
d) Steve jo
Answer: C
Explanation: In this program, We are breaking up the strings into the form of tokens.
19. How many parameters can a resize method take?
a) 1
b) 2
c) 1 or 2
d) 2
Answer: C
Explanation: There can be one or two parameters in resize method. They are string length and an optional new character to be inserted.
20. Which of the following is correct about remove_extent() function?
a) Removes the given dimension from an array
b) Removes the first dimension from the right of the array
c) Removes the first dimension from the left of the array
d) Removes the last dimension from the left of the array
Answer: C
Explanation: remove_extent() function removes the first dimension i.e. the first dimension from the given array.