10.7. Multiple Choice ExercisesΒΆ

Answer the following Multiple Choice questions to assess what you have learned in this chapter.

Q1

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.

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?

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] << ' ';
}

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?

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++) {
    for (const int& value: nums) {
        if (value % 2 == 0) {
           --value;
        }
        value *= 2;
    }
}

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};

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};

Suppose you have defined the bar function as the following

int bar (const vector<int>& vec, int num1, int num2) {
    int count = 0;
    for (const int& value: vec) {
        if (value/num1 == 0 && value/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 << bar(numbers, 2, 7);

Suppose you have defined the starts_with function as the following

int starts_with (const vector<string> & words, char c) {
    int count = 0;
    int pos = 0;
    for (size_t i = 0; i < words.size(); i++) {
    for (const int& word: words) {
        pos = word.find(' ');
        if (std::string::npos != pos && word[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 << haw_many(names, 'M');

Suppose you have defined the how_many function as the following

int how_many (const vector<string>& words, char let) {
    int count = 0;
    for (const auto& word: words) {
        for (const auto& c: word) {
            if (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 (const auto& c: letters) {
    counts.push_back(how_many(snacks, c));
}

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);