In: Computer Science
1. What is the output of the following code:
string s1 = “X”;
string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”;
cout << s2;
2. What is the output of the following code:
string s1 = “X”;
string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”;
cout << s[0] + s[3];
3. What is the output of the following code:
string s1 = “X”;
string s2 = “A” + s1 + “BC” + s1 + “DEF” + s1 + “G”;
cout << “Length is : “ << s.length());
4. What is the output of the following code:
string s1 = “ABC”;
string s2 = “DEF”;
string s3;
s3 = s1 + “ “ + s2;
cout << s3;
1) Answer: AXBCXDEFXG
Explanation:
Program:
Explanation:
Line 1: //Header file
Line 2: //declarative region
Line 3: //Start of main()
Line 4: // Here Initializing the string variable called s1
Line 5:// Here Initializing the string variable called s2
Line 6:// // Here Printing the Value of s2
Line 7://End of main()
Program:
#include <iostream>
using namespace std;
int main() {
string s1 = "X";
string s2 = "A" + s1 + "BC" + s1 + "DEF" + s1 + "G";
cout << s2;
}
Output:
2) Answer: 132
Explanation:
Program:
Explanation:
Line 1: //Header file
Line 2: //declarative region
Line 3: //Start of main()
Line 4: // Here Initializing the string variable called s1
Line 5:// Here Initializing the string variable called s2
Line 6://Here declaring the string variable called s3
Line 7://Here calculating the s2[0] value s2[3] value and adding
these two and Printin the result
Line 8://End of main()
Program:
#include <iostream>
using namespace std;
int main() {
string s1 = "X";
string s2 = "A" + s1 + "BC" + s1 + "DEF" + s1 + "G";
cout << s2[0] + s2[3];
}
Output:
3) Answer: Length is : 10
Explanation:
Program:
Explanation:
Line 1: //Header file
Line 2: //declarative region
Line 3: //Start of main()
Line 4: // Here Initializing the string variable called s1
Line 5:// Here Initializing the string variable called s2
Line 6:// // Here Printing the Length of s2
Line 7://End of main()
Program:
#include <iostream>
using namespace std;
int main() {
string s1 = "X";
string s2 = "A" + s1 + "BC" + s1 + "DEF" + s1 + "G";
cout << "Length is : " << s2.length();
}
Output:
4)Answer: ABC DEF
Explanation:
Program:
Explanation:
Line 1: //Header file
Line 2: //declarative region
Line 3: //Start of main()
Line 4: // Here Initializing the string variable called s1
Line 5:// Here Initializing the string variable called s2
Line 6://Here declaring the string variable called s3
Line 7://Here calculating the s3 value by adding the two strings
seperated by Space
Line 8:// Here Printing the Length of s3
Line 9://End of main()
Program:
#include <iostream>
using namespace std;
int main() {
string s1 = "ABC";
string s2 = "DEF";
string s3;
s3 = s1 + " " + s2;
cout << s3;
}
Output: