11.10. Multiple Choice ExercisesΒΆ
Answer the following Multiple Choice questions to assess what you have learned in this chapter.
Q1
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 x = random ();
Q2
Suppose you have defined the fizz_buzz function as the following
int fizz_buzz (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 << fizz_buzz(numbers, 2, 7);
Q3
Suppose you have defined the starts_with function as the following
int starts_with (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 << how_many(names, 'M');
Q4
Suppose you have defined the how_many function as the following
int how_many (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(how_many(snacks, letters[i]));
}
Q5
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);