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.
-
cout << " Bears!" << '\n'; -
cout << "Bears!" << '\n'; -
cout << "Lions &" << '\n'; -
cout << "Lions &"; -
cout << "Oh my!"; -
cout << "Tigers &" << '\n'; -
cout << "Tigers &"; -
cout << '\n'; -
int main() { -
}
Q2
Construct a block of code that swaps the value of integers x and y, which have values 3 and 6, respectively.
-
int main() { -
int temp = x; -
int x = 3 #distractor int y = 6 -
int x; int y; -
x = 3; y = 6; -
x = y; y = temp; -
x = y; #distractor y = x; -
}
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.
-
char crush = 'S'; -
char crush = 'S'; -
double GPA; GPA = 3.98; -
int GPA = 3.98; -
int main() { -
string name = "Dan Humphrey"; -
string name; name = Dan Humphrey; -
}
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.
-
double cups = 16 * gallons; -
double cups; 16 * gallons = cups; -
double gallons = 0.01 -
double gallons = 0.01; -
double tbsp = 16 * cups -
double tbsp; tbsp = 16 * cups; -
int main() { -
}
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.
-
cout << volume; -
int length = 2; int width = 3; int height = 4; -
int main() { -
int volume = length * width * height #distractor -
int volume; -
length = 2; width = 3; height = 4; -
print (volume) #distractor -
volume = height * width * length; -
}
Q6
Construct a block of code that changes the value of the variable a from the character 'a' to the character 'z'.
-
a = a + 25; -
a = a + 26; -
a = a + 27; -
char a = 'a'; -
char a = 'a'; -
int main() { -
}
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.
-
base = 3.14 * 3 * 3; #distractor -
base = 3.14 * 3 ^ 2; #distractor -
cout << 3.14 * 3 * 3 * 4; -
cout << 3.14 * 3 ^ 2 * 4; #distractor -
cout << base * height; #distractor -
cout << volume; #distractor -
height = 4; #distractor -
int main() { -
volume = base * height; #distractor -
}
Q8
Construct a block of code that prints "My favorite class is MATH" on the same line.
-
cout << "My favorite class is " << '\n'; cout << favorite; -
cout << "My favorite class is "; cout << favorite; -
cout << "My favorite class is" << favorite; -
int main() { -
string favorite = "MATH"; -
string favorite = 'MATH'; -
}
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?
-
double discount = game * 0.60; -
double discount = game - (0.60 * game); -
double game = $359.99; -
double game = 359.99; -
double saved = discount; -
double saved = game + discount; -
double saved = game - discount; -
int main() { -
}
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.
-
int main() { -
name = "Buddy" -
name = "Buddy"; -
name = "Copper"; -
string name = "Champ"; -
string name = "Copper"; -
string name = "Higgins"; -
string name = 'Champ'; -
string new_name = "Higgins"; name = new_name; -
}
Q11
Construct a block of code that prints the remainder of 18 when divided by 13.
-
cout << x % y; -
cout << y % x; -
cout << y / x; -
int main () { -
int x = 18; int y = 13; -
}