2.15. Mixed-Up Code Exercises

Answer the following Mixed-Up Code questions to assess what you have learned in this chapter.

Q1

Construct a block of code that prints: "Lions &" one the first line, "Tigers & Bears!" on the second line, and "Oh my!" on the FOURTH line.

  1. cout << " Bears!" << '\n';
  2. cout << "Bears!" << '\n';
  3. cout << "Lions &" << '\n';
  4. cout << "Lions &";
  5. cout << "Oh my!";
  6. cout << "Tigers &" << '\n';
  7. cout << "Tigers &";
  8. cout << '\n';
  9. int main() {
  10. }

Q2

Construct a block of code that swaps the value of integers x and y, which have values 3 and 6, respectively.

  1. int main() {
  2. int temp = x;
  3. int x = 3 #distractor
    int y = 6
  4. int x;
    int y;
  5. x = 3;
    y = 6;
  6. x = y;
    y = temp;
  7. x = y; #distractor
    y = x;
  8. }

Q3

Dan Humphrey is a 3.98 student at Constance High School. His crush's first initial is S. Construct a program that assigns the variables name, GPA, and crush, in that order.

  1. char crush = 'S';
  2. char crush = 'S';
  3. double GPA;
    GPA = 3.98;
  4. int GPA = 3.98;
  5. int main() {
  6. string name = "Dan Humphrey";
  7. string name;
    name = Dan Humphrey;
  8. }

Q4

You decide to make homemade Mac 'n' Cheese for you and your roomates. Whoever wrote the recipe wanted to make things hard for you by stating that it calls for 1% of a gallon of milk. Construct a block of code that converts this to tablespoons.

  1. double cups = 16 * gallons;
  2. double cups;
    16 * gallons = cups;
  3. double gallons = 0.01
  4. double gallons = 0.01;
  5. double tbsp = 16 * cups
  6. double tbsp;
    tbsp = 16 * cups;
  7. int main() {
  8. }

Q5

Construct a block of code that takes the volume of the rectangular prism defined by length, width, and height and prints the result to the terminal.

  1. cout << volume;
  2. int length = 2;
    int width = 3;
    int height = 4;
  3. int main() {
  4. int volume = length * width * height #distractor
  5. int volume;
  6. length = 2;
    width = 3;
    height = 4;
  7. print (volume) #distractor
  8. volume = height * width * length;
  9. }

Q6

Construct a block of code that changes the value of the variable a from the character 'a' to the character 'z'.

  1. a = a + 25;
  2. a = a + 26;
  3. a = a + 27;
  4. char a = 'a';
  5. char a = 'a';
  6. int main() {
  7. }

Q7

Construct a block of code that outputs the volume of a cylinder with a radius of 3 and a height of 4. There are many ways to do this using the choices below, but only the correct answer that uses the LEAST lines of code will be accepted.

  1. base = 3.14 * 3 * 3; #distractor
  2. base = 3.14 * 3 ^ 2; #distractor
  3. cout << 3.14 * 3 * 3 * 4;
  4. cout << 3.14 * 3 ^ 2 * 4; #distractor
  5. cout << base * height; #distractor
  6. cout << volume; #distractor
  7. height = 4; #distractor
  8. int main() {
  9. volume = base * height; #distractor
  10. }

Q8

Construct a block of code that prints "My favorite class is MATH" on the same line.

  1. cout << "My favorite class is " << '\n';
    cout << favorite;
  2. cout << "My favorite class is ";
    cout << favorite;
  3. cout << "My favorite class is" << favorite;
  4. int main() {
  5. string favorite = "MATH";
  6. string favorite = 'MATH';
  7. }

Q9

It's Black Friday and the Nintendo Switch you've been saving up for is marked down to 60% of its original price! Construct a block of code that calculates how much money you'd be saving if the system originally cost $359.99?

  1. double discount = game * 0.60;
  2. double discount = game - (0.60 * game);
  3. double game = $359.99;
  4. double game = 359.99;
  5. double saved = discount;
  6. double saved = game + discount;
  7. double saved = game - discount;
  8. int main() {
  9. }

Q10

Your family just bought a dog and everyone has been fighting over what to name it. It went from Champ to Copper to Higgins, and after a few days of being Higgins, everyone agreed on Buddy. Construct a block of code that illustrates this concept.

  1. int main() {
  2. name = "Buddy"
  3. name = "Buddy";
  4. name = "Copper";
  5. string name = "Champ";
  6. string name = "Copper";
  7. string name = "Higgins";
  8. string name = 'Champ';
  9. string new_name = "Higgins";
    name = new_name;
  10. }

Q11

Construct a block of code that prints the remainder of 18 when divided by 13.

  1. cout << x % y;
  2. cout << y % x;
  3. cout << y / x;
  4. int main () {
  5. int x = 18;
    int y = 13;
  6. }