7.17. Multiple Choice ExercisesΒΆ
Q1
Which variables below is declared as a string type?
int main() {
int x = 0;
double y = 4.5;
string word = "hello";
string letter = "a";
char c = 'c';
bool is_prime = 1;
}
Q2
What value should replace the question mark to output the character 'p'?
int main() {
string quote = "Not my tempo.";
cout << quote[?];
}
Q3
What is the output of the code below?
int main() {
string quote = "I love you 3000.";
int x = 3;
int y = 3 * x;
int z = 1;
if (y > 12) {
z = z + x + y;
}
else {
z = z + y - x;
}
cout << quote[z];
}
Q4
What is the output of the code below?
int main() {
string quote = "Look at me. I'm the captain now.";
int x = quote.size();
cout << quote[x];
}
Q5
What is the output of the code below?
int main() {
string quote = "With great power comes great responsiblity.";
int n = 0;
while (n < quote.size()) {
if (n % 5 == 0) {
cout << quote[n];
}
n++;
}
}
Q6
What is the output of the code below?
int main() {
string quote = "Why so serious?";
int index = quote.find('a');
cout << index;
}
Q7
What is the output of the code below?
int main() {
string tongue_twister = "How much wood could a woodchuck chuck if a woodchuck could chuck wood?";
int index = quote.find("wood");
cout << index;
}
Q8
What is the output of the code below?
int main() {
string tongue_twister = "How much wood could a woodchuck chuck if a woodchuck could chuck wood?";
int index = find (quote, 'w', quote.find("wood") + 1);
cout << index;
}
Q9
What is the output of the code below?
int main() {
string quote = "Life is like a box of chocolates. You never know what you're gonna get.";
int i = 0;
int count = 0;
while (i < quote.size()) {
if (quote[i] == 'e') {
++count;
}
++i;
}
cout << count;
}
Q10
What is the output of the code below?
int main() {
string call = "Marco!";
string response = "Polo!";
string output = "call" + "response";
cout << output;
}
Q11
An error occured while delivering a message. All instances of the letter 's' got replaced by 'X's. Can you complete the code below to fix this error by selecting the correct line of code to replace the question marks?
int main() {
string question = "Honey? Where'X my Xuper Xuit?";
int i = 0;
while (i < question.size()) {
if (question[i] == 'X') {
?????
}
i++;
}
cout << question;
}
Q12
What is the output of the code below?
cout << ("butter" < "butterfly");
Q13
What is the output of the code below?
int main() {
string quote = "Suffering builds character";
int count = 0;
int index = 17;
while ( index != quote.size() ){
if ( quote[index] == 'a' || quote[index] == 'e' ){
count = count + index;
}
index = index + 1;
}
cout << count << '\n';
}
Q14
What is the output of the code below?
int main() {
string quote = "Its Bond, James Bond";
int index = 1;
while( index < quote.size() ){
quote[index] = 'M';
index = index * 2;
}
cout << quote << '\n';
}