.. _random-numbers-multiple-choice-exercises: Multiple Choice Exercises ------------------------- Answer the following **Multiple Choice** questions to assess what you have learned in this chapter. .. tb-group:: :name: self_check .. tb-tab:: Q1 .. tb-choice:: :name: random_mc1 Suppose you are randomly assigning students to discussions 1-8. How would you do this correctly? Assume you have alreay implemented the following code. .. code-block:: int x = random (); - [x] .. code-block :: 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. - [ ] .. code-block :: int y = x % 8; y = y + 1; - The first part creates a random number between 0 and 8 (9 numbers). This is too many. - [ ] .. code-block :: int y = x % 7; - This creates a random number between 0 and 7 (8 numbers), which are not the numbers we are looking for. - [ ] .. code-block :: int y = x % 8; - 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. .. tb-tab:: Q2 .. tb-choice:: :name: random_mc2 Suppose you have defined the ``fizz_buzz`` function as the following .. code-block:: int fizz_buzz (const vector & 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? .. code-block:: vector numbers = {6, 8, 14, 21, 28, 35, 36, 42, 49, 70, 81, 98}; cout << fizz_buzz(numbers, 2, 7); - [ ] 1 - 14 is 7 * 2. Don't forget about the other multiples of 7 and 2. - [ ] 2 - See if you can find the other multiples of 7 and 2. - [ ] 3 - See if you can find the other multiples of 7 and 2. - [ ] 4 - See if you can find the other multiples of 7 and 2. - [x] 5 + 14, 28, 42, 70, and 98 are all multiples of 7 and 2 and are counted by ``fizz_buzz``. .. tb-tab:: Q3 .. tb-choice:: :name: random_mc3 Suppose you have defined the ``starts_with`` function as the following .. code-block:: int starts_with (const vector & 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? .. code-block:: vector names = {"Ross Meldrum", "Monica Morrissey", "Maria Geller", "Marty Bing"}; cout << how_many(names, 'M'); - [ ] 1 - Is the function counting how many first and last ``names`` begin with ``"M"``? - [x] 2 + The function is counting how many last names begin with ``"M"``. - [ ] 3 - Is the function counting how many first ``names`` begin with ``"M"``? - [ ] 4 - Is the function counting how many elements in ``names`` contain ``"M"``? - [ ] 5 - Is the function counting how many times ``"M"`` appears in ``names``? .. tb-tab:: Q4 .. tb-choice:: :name: random_mc4 Suppose you have defined the ``how_many`` function as the following .. code-block:: int how_many (const vector& 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? .. code-block:: vector snacks = {"cheetos", "ruffles", "jalepeno chips", "oreos", "m&ms"}; vector letters = {'a', 'e', 'i', 'o', 'u'}; vector counts = {}; for (int i = 0; i < letters.size(); ++i) { counts.push_back(how_many(snacks, letters[i])); } - [ ] {1, 6, 2, 6, 2} - What is being counted in ``counts``? - [ ] {1, 4, 1, 3, 1} - ``counts`` isn't a count of how many words each vowel appers in inside ``snacks``. - [x] {1, 6, 1, 4, 1} + ``counts`` contains a count of how many times each vowel appers in ``snacks``. - [ ] {1, 6, 2, 3, 2} - What is being counted in ``counts``? - [ ] Error! - There isn't anything wrong with the code that would cause an error. .. tb-tab:: Q5 .. tb-choice:: :name: random_mc5 Suppose you have defined the ``repeater`` function as the following .. code-block:: int repeater (const vector& 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? .. code-block:: vector vals = {1,1,3,2,2,3,3,4,5,6,7,4,4,5}; int counter = repeater(vals); - [ ] 169 - What is being counted by ``repeater`` in each iteration of the outer loop? Definitely not everything! - [ ] 32 - ``repeater`` does count repeats but does it consider values at the same indexes repeats? - [x] 18 + ``repeater`` considers the number of times each index shares a value with any of the other indices. - [ ] 13 - ``repeater`` dosen't simply count the number of elements.