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 10.16 Multiple Choice Exercises
Answer the following
Multiple Choice questions to assess what you have learned in this chapter.
1.
Suppose you are collecting data for a science experiment. You are to perform three trials of eight temperature readings measured in degrees fahrenheit to the nearest hundredth and initialized to
freezing . Choose the vector that has the proper amount of storage for this scenario.
vector<int> temps (24, 32.00);
We canβt declare the vector as an integer type because all temperature readings will be truncated.
vector<double> temps (24, 32.00);
First comes the vector size, then the initial values.
vector<double> temps (0.00, 24);
Freezing is 32 degrees fahrenheit. The order of parameters is incorrect.
vector<double> temps (32, 24.00);
This statement creates a vector size 32 with elements initialized to 24.00 degress fahrenheit.
2.
Suppose the following code is run:
vector<string> lauren = {"happy", "to", "you", "September", "birthday", "girl"}
How would you save the string
"birthday"
from
lauren
to the variable
nurse
?
Vectors are zero-indexed, so the fifth element is the fourth index.
Remember, vectors are zero-indexed!
Remember, vectors are zero-indexed!
This is not proper vector indexing.
This is not proper vector indexing. Also, vectors are zero-indexed.
3.
What gets printed when the following code is run:
vector<string> chant = {"Hail", "to", "the", "victors", "valiant"};
chant[0]=chant[3];
chant[3]=chant[0];
chant[1]=chant[0];
for ( size_t i = 0; i < chant.size(); i++ ) {
cout << chant[1][i] << " ";
}
victors victors the victors valiant
Although this is the final version of chant
, we are not printing chant
!
error! we run into an error somewhere in the execution due to an out of bounds access.
Remember, chant
at index 1 is no longer βhailβ.
You are thinking of the correct word but consider upto what index we print.
Correct! we print the first 5 letters of the string at index 1 which is βvictoβ.
4.
Select all of the following statments that correctly make a copy of
lauren
.
vector<string> lauren = {"happy", "to", "you", "September", "birthday", "girl"}
How would you save the string
"birthday"
from
lauren
to the variable
nurse
?
vector<string> harry (lauren)
This syntax is correct, but isnβt used often.
vector<string> lauren (ella)
You make a copy of the vector in parentheses.
vector<string> lauren = mariah
Remember how assignment statements work!
vector<katie> string = lauren
This is not proper syntax.
vector<string> mariah = lauren
This is the most common syntax.
5.
What is the value of nums after the following code executes?
int main() {
vector<int> nums = {0, 8, 5, 1, 4, 3};
for (int i = 0; i < 6; i++) {
if (nums[i] % 2 == 0) {
nums[i]--;
}
nums[i] = nums[i] * 2;
}
cout << nums[1];
}
nums
is modified inside of the loop.
Take a look at the conditional.
Take a closer look at the conditional.
All even numbers were decremeneted, then all numbers were multiplied by 2.
Take a closer look at what happens inside of the conditional.
6.
Multiple Response Select all ways to print out the contents of
ryan
without going out of bounds.
vector<int> ryan = {2, 3, 1, 5, 6, 0, 0, 5, 4};
for (int i = 0; i < ryan.size(); ++i) {
cout << ryan[i] << " ";
}
When we deal with the size
function, we canβt use type int
.
for (size_t j = 0; j < ryan.size(); j++) {
cout << ryan[j] << " ";
}
When we deal with the size
function, we must use type size_t
.
for (int k = 0; k < 8; ++k) {
cout << ryan[k] << " ";
}
There are 9 elements, numbered 0 through 8, but here we only iterate through 8 of them.
for (int n = 0; n < 9; n++) {
cout << ryan[n] << " ";
}
There are 9 elements numbered 0 through 8, and this statement iterates over all of them.
for (int m = 0; m <= 8; ++m) {
cout << ryan[m] << " ";
}
There are 9 elements numbered 0 through 8, and this statement iterates over all of them.
7.
Suppose you want
ryan
to have the value
vector<int> ryan = {2, 3, 1, 5, 6, 7, 8, 9};
What vector functions will you use to achieve this, and how many times will you use them? Keep in mind,
ryan
is currently the following vector of integers.
vector<int> ryan = {2, 3, 1, 5, 6, 0, 0, 5, 4};
Use
push_back
4 times with no arguments to get rid of the last 4 elements, then use
push_back
3 times with arguments to specify which values you want to add to the end.
Youβll need to use two different functions to accomplish this task.
Use
push_back
4 times with no arguments to get rid of the last 4 elements, then use
pop_back
3 times with arguments to specify which values you want to add to the end.
push_back
pushes new items onto the end of the vector, and pop_back
pops old items off the end of the vector.
Use
pop_back
4 times with no arguments to get rid of the last 4 elements, then use
pop_back
3 times with arguments to specify which values you want to add to the end.
Youβll need to use two different functions to accomplish this task.
Use
pop_back
4 times with no arguments to get rid of the last 4 elements, then use
push_back
3 times with arguments to specify which values you want to add to the end.
push_back
pushes new items onto the end of the vector, and pop_back
pops old items off the end of the vector.
8.
Suppose you are randomly assigning students to discussions 1-8. How would you do this correctly? Assume you have alreay implemented the following code.
int y = x % 7;
y = y + 1;
The first part creates a random number between 0 and 7 (8 numbers) and the second part adds 1 so that our random number is actually between 1 and 8.
int y = x % 8;
y = y + 1;
The first part creates a random number between 0 and 8 (9 numbers). This is too many.
This creates a random number between 0 and 7 (8 numbers), which are not the numbers we are looking for.
The first part creates a random number between 0 and 8 (9 numbers). This is too many, and not the numbers we are looking for.
9.
Suppose you have defined the
fizzBuzz
function as the following
int fizzBuzz(const vector<int> & vec, int num1, int num2) {
int count = 0;
for (size_t i = 0; i < vec.size(); i++) {
if (vec[i] % num1 == 0 && vec[i] % num2 == 0) {
count++;
}
}
return count;
}
What would be printed in the following case?
vector<int> numbers = {6, 8, 14, 21, 28, 35, 36, 42, 49, 70, 81, 98};
cout << fizzBuzz(numbers, 2, 7);
14 is 7 * 2. Donβt forget about the other multiples of 7 and 2.
See if you can find the other multiples of 7 and 2.
See if you can find the other multiples of 7 and 2.
See if you can find the other multiples of 7 and 2.
14, 28, 42, 70, and 98 are all multiples of 7 and 2 and are counted by fizzBuzz
.
10.
Suppose you have defined the
startsWith
function as the following
int startsWith(const vector<string> & vec, char c) {
int count = 0;
int pos = 0;
for (size_t i = 0; i < vec.size(); i++) {
pos = vec[i].find(" ");
if (vec[i][pos + 1] == c) {
count++;
}
}
return count;
}
What would be printed in the following case?
vector<string> names = {"Ross Meldrum", "Monica Morrissey", "Maria Geller", "Marty Bing"};
cout << howMany(names, 'M');
Is the function counting how many first and last names
begin with "M"
?
The function is counting how many last names begin with "M"
.
Is the function counting how many first names
begin with "M"
?
Is the function counting how many elements in names
contain "M"
?
Is the function counting how many times "M"
appears in names
?
11.
Suppose you have defined the
howMany
function as the following
int howMany(const vector<string>& vec, char let) {
int count = 0;
for (size_t i = 0; i < vec.size(); i++) {
for (size_t c = 0; c < vec[i].size(); c++) {
if (vec[i][c] == let) {
count++;
}
}
}
return count;
}
What is the value of counts after the following code is run?
vector<string> snacks = {"cheetos", "ruffles", "jalepeno chips", "oreos", "m&ms"};
vector<char> letters = {'a', 'e', 'i', 'o', 'u'};
vector<int> counts = {};
for (int i = 0; i < letters.size(); ++i) {
counts.push_back(howMany(snacks, letters[i]));
}
What is being counted in counts
?
counts
isnβt a count of how many words each vowel appers in inside snacks
.
counts
contains a count of how many times each vowel appers in snacks
.
What is being counted in counts
?
There isnβt anything wrong with the code that would cause an error.
12.
Suppose you have defined the
repeater
function as the following
int repeater(const vector<int>& vec) {
int count = 0;
for (size_t i = 0; i < vec.size(); i++) {
for (size_t j = 0; j < vec.size(); j++) {
if ((vec[j] == vec[i]) && (i != j)) {
count++;
}
}
}
return count;
}
What is the value of counter after the following code is run?
vector<int> vals = {1,1,3,2,2,3,3,4,5,6,7,4,4,5};
int counter = repeater(vals);
What is being counted by repeater
in each iteration of the outer loop? Definitely not everything!
repeater
does count repeats but does it consider values at the same indexes repeats?
repeater
considers the number of times each index shares a value with any of the other indices.
repeater
dosenβt simply count the number of elements.
You have attempted
of
activities on this page.