q19) CONSIDER THE FOLLOWING CODE: void Question() { string x = \"12\"; mystery1(
ID: 3810046 • Letter: Q
Question
q19)
CONSIDER THE FOLLOWING CODE:
void Question()
{
string x = "12";
mystery1(x);
string str1 = "21";
mystery2(str1);
string str2 = "11";
mystery2(str2);
string str3 = "31";
mystery2(str3);
string str4 = "91";
mystery2(str4);
string str5 = "81";
mystery1(str5);
x = x + str1 + str2 + str3 + str4 + str5;
cout << x;
}
////////////////////////////////////////////////////////////
void mystery1(string &a)
{
int num = a.length();
string temp;
for (int i = 0 ; i < num ; i++)
{
temp = a.at(i) + temp;
}
a = temp;
}
////////////////////////////////////////////////////////////
void mystery2(string &b)
{
int num = b.length();
string temp ="";
for (int i = 0; i < num; i++)
{
if(b.at(i) == '1')
temp = temp + b.at(i);
}
b = temp;
}
Select one:
a. 122111319181
b. 21329181
c. 211111118
d. 123181
please explain how you get the answer. Thank you
Explanation / Answer
Answer is (c). 211111118
Logic:
Here, we need to write the result of 'x' after the operations are done.
One thing to notice first, functions mystery1 and mystery2 both are taking a parameter using reference (&). This means that whatever modifications are done in this method, they reflect back to the variable in the method Question().
mystrery1() method:
In this method, a string is passed. Its length is found first. Then from 0 to length-1 index the for loop is iterating because the starting index of string is 0. at(i) gives the character at ith place like at(0) is the first character. temp is initially null i.e . "". So, in the first run, the first character of string is stored in temp. Now comes the i=1.
So, 2nd character is taken from the string and it is written ahead of previous temp value which was first character. Now the temp becomes, 2nd then 1st character. The same process is continued
For instance, say you pass the string 146. At first, temp = "". After for loop runs first time, it takes 1 from this string and puts ahead of temp which was "" initially. So, temp becomes "1". Now, i=1 and at(1) value is 4. So, 4 is placed ahead of previous temp which was "1". Here + serves as string concatenation. So, temp now becomes "41". Again i=2 and it is less then length 3 so next character is taken which is 6 and is placed ahead of temp so now temp becomes "641" which means at the end, this function returns the string in its reverse format.
mystery2() function:
This function also uses the same loop as above, just 1 thing is changed: it stores only the characters where the character is "1" so if you pass there 121, in the first iteration, 1 is stored, in second iteration, 2 != 1 so it is not stored and again in the third iteration, the character is stored. So the string becomes "11".
So, this function modifies the strings and writes the 1s that were present in the original string.
So now let's trace the program and find the value of x.
string x = "12";
mystery1(x);
This calls mystery1("12") so its reverse is returned and stored in x itself as reference is used. So, x = "21".
string str1 = "21";
mystery2(str1);
This calls mystery2("21") which returns "1" because there is one time 1 present in str1 and stores in the str1. So, str1 = "1"
string str2 = "11";
mystery2(str2);
This calls mystery2("11") which returns "11" because there is two times 1 present in str2 and stores in the str2. So, str2 = "11".
string str3 = "31";
mystery2(str3);
This calls mystery2("31") which returns "1" because there is one time 1 present in str3 and stores in the str3. So, str3 = "1".
string str4 = "91";
mystery2(str4);
This calls mystery2("91") which returns "1" because there is one time 1 present in str4 and stores in the str4. So, str4 = "1".
string str5 = "81";
mystery1(str5);
This calls mystery1("81") which returns "18" and stores in the str5. So, str5 = "18".
So, as all the values are found, next line is:
x = x + str1 + str2 + str3 + str4 + str5; which means
x = "21" + "1" + "11" + "1" + "1" + "18".
So, this is concatenation operation makes x = 211111118 which is printed. So the correct option is option (C).
Please comment if there is any query. Thank you. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.