Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ String Aptitude Questions and Answers
C++ String Aptitude: This section contains C++ String Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 21, 2021
1) There are the following statements that are given below, which of them are correct about string in C++?
- A string is the sequence of characters.
- In C++, the string is a predefined class. This is used to perform string operations.
- In C++, we can also use character array as well as string class.
- All of the above
Options:
- A and B
- A and C
- B and C
- D
Correct Answer - 4
D
Explanation:
All given statements are correct about string in C++.
2) What is the correct output of the given code?
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str1 = "ABC";
string str2;
strcpy(str2, str1);
cout << str2 << endl;
return 0;
}
Options:
- ABC
- Syntax error
- Garbage value
- Runtime error
Correct Answer - 2
Syntax error
Explanation:
The above code will generate a syntax error.
ain.cpp: In function ‘int main()’:
main.cpp:10:22: error: cannot convert ‘std::string {aka std::basic_string}’ to ‘char*’ for argument ‘1’
to ‘char* strcpy(char*, const char*)’
strcpy(str2, str1);
^
3) What is the correct output of the given code?
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str1 = "ABC";
string str2;
str1.copy(str2);
cout << str2 << endl;
return 0;
}
Options:
- ABC
- Syntax error
- Garbage value
- Runtime error
Correct Answer - 2
Syntax error
Explanation:
The above code will generate a syntax error.
main.cpp: In function ‘int main()’:
main.cpp:10:19: error: no matching function for call to ‘std::basic_string::copy(std::string&)’
str1.copy(str2);
^
4) What is the correct output of the given code?
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str1 = "ABC";
char str2[4];
str1.copy(str2, 3);
cout << str2 << endl;
return 0;
}
Options:
- ABC
- Syntax error
- Garbage value
- Runtime error
Correct Answer - 1
ABC
Explanation:
The above code will print "ABC" on the console screen.
5) What is the correct output of the given code?
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str1 = "ABC";
string str2;
str2 = str1;
cout << str2 << endl;
return 0;
}
Options:
- ABC
- Syntax error
- Garbage value
- Runtime error
Correct Answer - 1
ABC
Explanation:
The above code will print "ABC" on the console screen.
6) What is the correct output of the given code?
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str1 = "ABC";
string str2;
str2 = str1;
cout << str2.length();
return 0;
}
Options:
- 3
- Syntax error
- Garbage value
- Runtime error
Correct Answer - 1
3
Explanation:
The above code will print "3" on the console screen.
7) Which of the following function is used to remove all characters of a string object in C++?
- removeall()
- remove()
- clear()
- delete()
Correct Answer - 3
clear()
Explanation:
The clear() function is used to remove all characters of a string object in C++.
8) What is the correct output of the given code?
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char ch[4] = "ABC";
string str;
strcpy((char*)str, ch);
cout << str << endl;
return 0;
}
Options:
- ABC
- Syntax error
- Garbage value
- Runtime error
Correct Answer - 2
Syntax error
Explanation:
The above code will generate a syntax error.
9) What is the correct output of the given code?
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char ch[4] = "ABC";
string str;
for (int i = 0; i < 3; i++)
str += ch[i];
cout << str << endl;
return 0;
}
Options:
- ABC
- Syntax error
- Garbage value
- Runtime error
Correct Answer - 1
ABC
Explanation:
The above code will print "ABC" on the console screen.
10) What is the correct output of the given code?
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str = "Hello World";
;
char ch;
ch = str.at(4);
cout << ch << endl;
return 0;
}
Options:
- l
- o
- Syntax error
- Runtime error
Correct Answer - 2
o
Explanation:
The above code will print 'o' on the console screen.
11) What is the correct output of the given code?
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string str = "Hello World";
cout << str.substr(6) << endl;
return 0;
}
Options:
- World
- Garbage value
- Syntax error
- Runtime error
Correct Answer - 1
World
Explanation:
The above code will print "World" on the console screen.
12) The substr() function is overloaded?
- Yes
- No
Correct Answer - 1
Yes
Explanation:
The substr() function is overloaded.
13) Which of the following function is used to concatenate a string to another string object in C++?
- concat()
- strcat()
- append()
- None of the above
Correct Answer - 3
append()
Explanation:
The append() method is used to concatenate string.
14) Which of the following function is used to change characters in string object from the specified index?
- change()
- update()
- edit()
- replace()
Correct Answer - 4
replace()
Explanation:
The replace() function is used to change characters in string object from the specified index.
15) Which of the following function is used to get the index of a specified character in a string object?
- get()
- search()
- loc()
- find()
Correct Answer - 4
find()
Explanation:
The find() function is used to get the index of a specified character in a string object.