Question

In: Computer Science

1.   What is the output of the following code: string s1 = “X”; string s2 =...

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;

Solutions

Expert Solution

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:


Related Solutions

True or False) The following code will output "I was true". bool isGreater(string s1, string s2)...
True or False) The following code will output "I was true". bool isGreater(string s1, string s2) { if(s1 > s2) { return true; } return false; } int main() { if(isGreater("before","back")) { cout << "I was true"; } else { cout << "I was false"; } return 0; }
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings...
Code in c# Write a recursive method called isReverse(String s1, String s2) that accepts two strings as parameters and returns true if the two strings contain the same sequence of characters as each other but in the opposite order and false otherwise. • The recursive function should ignore capitalization. (For example, the call of isReverse("hello", "eLLoH") would return true.) • The empty string, as well as any one letter string, should be its own reverse. Write a driver program that...
(10 marks) Write a function to check whether string s1 is a substring of string s2....
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2).
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
What is the output of the following code: x = 0 a = 1 b =...
What is the output of the following code: x = 0 a = 1 b = -3 if a > 0:    if b < 0:       x = x + 5    elif a > 5:       x = x + 4    else:       x = x + 3 else:     x = x + 2 print(x) 4 2 5 3 Consider the following code segment:    sum = 0.0    while True:       number = input(“Enter a...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists some probability for an atom in S2 to decay to S1. What actually causes the atom to decay to the lower energy state? is it the fact that the lower state is more probable for the atom to be in as given by the Boltzmann Factor? so since it is more probable, it has more microstates and entropy causes it to decay? Please help...
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition...
Answer the following Discrete Structures Suppose string s = (s1 s2 ..sn). Give a recursive definition of the function numOnes(n), which counts the number of 1s in bit-string of length n, Make sure to define the function for the base case, numOnes(0).
Suppose x is 1 and y is 10. What is the output of the following code?...
Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) && ( y <11)                                     Display statement                         else                                     Display nothing 2. Suppose x is 1 and y is 10. What is the output of the following code?                         If (x> 1) || ( y <11)                                     Display statement                         Else                                     Display nothing 3. Write a java program that prints 1 to infinite numbers. 4. The...
Write the following Python code: A string X is an anagram of string Y if X...
Write the following Python code: A string X is an anagram of string Y if X can be obtained by arranging all characters of Y in some order, without removing any characters and without adding new characters. For example, each of the strings "baba", "abab", "aabb" and "abba" is an anagram of "aabb", and strings "aaab", "aab" and "aabc" are not anagrams of "aabb". A set of strings is anagram-free if it contains no pair of strings which are anagrams...
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2...
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2 or padding it with extra null characters as necessary. The target string may not be null-terminated if the length of s2 is n or more. The function returns s1. Write your own version of this function. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT