Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Exercises 7.17 Multiple Choice Exercises
1.
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 isPrime = 1;
}
2.
What value should replace the question mark to output the character βpβ?
int main() {
string quote = "Not my tempo.";
cout << quote[?];
}
Remember that indexing begins at 0 in C++.
βpβ is located at index 10 in quote.
The character βmβ is located at index 4.
The character βNβ is located at index 0.
3.
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];
}
The value of z
is not 0.
The value of z
is not greater than 11.
The value of z
is not 3.
The final value of z
is 7, and βyβ is at index 7 of quote
.
4.
What is the output of the code below?
int main() {
string quote = "Look at me. I'm the captain now.";
int x = quote.length();
cout << quote[x];
}
-1 is not in quote
.
x
is not the index value of the character βwβ.
x
is not the index value of the last period.
It might be logical to think that memory outside of the string
is empty space, but there could be leftover junk values.
Error, we are indexing out of bounds.
x
has a value of 32 and there is no index 32 in quote
.
5.
What is the output of the code below?
int main() {
string quote = "With great power comes great responsiblity.";
size_t n = 0;
while (n < quote.length()) {
if (n % 5 == 0) {
cout << quote[n];
}
n++;
}
}
Remember that indexing begins at 0 in C++.
If we print out every fifth character, including the first, this is the answer.
ith reatpowe coms grat rsponibliy.
This is what we would get if we removed every fifth character.
With great power comes great responsiblity.
Take a look at the conditional in the while loop.
6.
What is the output of the code below?
int main() {
string quote = "Why so serious?";
size_t index = quote.find("a");
cout << index;
}
Since βaβ is not found in quote
, the find
function returns string::npos
.
The character at index 0 is βWβ.
The character at index 8 is βeβ.
There is no index 15
in quote.
7.
What is the output of the code below?
int main() {
string quote = "How much wood could a woodchuck chuck if a woodchuck could chuck wood?";
size_t index = quote.find("wood");
cout << index;
}
Although βwoodβ appears four times in the string
, that is not what the find
function returns.
The index of βwβ in the first βwoodβ is at index 9.
Remember indexing begins at 0 in C++.
The find
function returns the index of the first character of the found string.
The find
function returns the index of the first character of the found string.
8.
What is the output of the code below?
int main() {
string quote = "How much wood could a woodchuck chuck if a woodchuck could chuck wood?";
size_t index = quote.find('w', quote.find("wood") + 1);
cout << index;
}
Take a closer look at the starting index for where we should start looking.
After the first βwβ, the second βwβ appears at index 22.
Take a closer look at the find
function and its arguments.
Take a closer look at the find
function and its arguments.
9.
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.";
size_t i = 0;
size_t count = 0;
while (i < quote.length()) {
if (quote[i] == 'e') {
count++;
}
i++;
}
cout << count;
}
Are there any occurences of the letter βeβ in quote
?
Count the number of βeβs in quote
.
There are 7 occurences of the letter βeβ in quote
.
Count the number of βeβs in quote
.
10.
What is the output of the code below?
int main() {
string call = "Marco!";
string response = "Polo!";
string output = "call" + "response";
cout << output;
}
Take a closer look at the initialization of output
.
Take a closer look at the initialization of output
.
Can we concatenate βcallβ and βresponseβ?
Can we concatenate βcallβ and βresponseβ?
We cannot concatenate native C strings like βcallβ and βresponseβ, so this code results in an error.
11.
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?";
size_t i = 0;
while (i < question.length()) {
if (question[i] == 'X') {
?????
}
i++;
}
cout << question;
}
The argument in the []
operator should be a position in the string.
Check the order of your assignment.
We cannot assign the value of βsβ to βXβ.
This will successfully replace all instances of βXβ with βsβ.
12.
What is the output of the code below?
int main() {
cout << ("butter" < "butterfly");
}
The operator between βbutterβ and βbutterflyβ is the <
operator, not <<
.
Does βbutterβ come before or after βbutterflyβ?
βbutterβ comes before βbutterflyβ in the dictionary.
In C++, boolean values are outputted as either a 0 or 1.
In C++, boolean values are outputted as either a 0 or 1.
13.
What is the output of the code below?
int main() {
string quote = "Suffering builds character";
size_t count = 0;
size_t index = 17;
while ( index != quote.length() ) {
if ( quote[index] == 'a' || quote[index] == 'e' ) {
count = count + index;
}
index = index + 1;
}
cout << count << endl;
}
The code is not counting the number of aβs or eβs after position 17. Rather adding up their indices.
The code is not counting the number of aβs or eβs. Rather adding up their indices.
Correct! the occurences of βaβ are 19 and 21, while that of βeβ is 24 (after index
17). The total is 64.
The first occurence of βeβ is at index 4 so it is not counted.
14.
What is the output of the code below?
int main() {
string quote = "Its Bond, James Bond";
size_t index = 1;
while( index < quote.length() ) {
quote[index] = 'M';
index = index * 2;
}
cout << quote << endl;
}
βIMM MondM James Mondβ
Correct! We change indices 1,2,4,8,16 to M before index
becomes >
quote.length()
.
βIMMMMMMMMMMMMMMMMMMMβ
We are not increasing index
by 1, instead we are doubling it.
βMMM MondM James Mondβ
We donβt start at position 0 this time.
βIMsMBMnM,MJMmMsMBMnMβ
we are not increasing index
by 2, instead we are doubling it.
You have attempted
of
activities on this page.