9.13. Multiple Choice Exercises¶
gatsby-
gatsbyis aBook. frankenstein-
frankensteinis aBook. flies-
fliesis astring. year-
yearis anint.
Q-1: Which of the following are variables of type Book?
struct Book {
string title, author;
int yearPublished;
double price;
};
int main() {
Book gatsby = { "The Great Gatsby", "F. Scott Fitzgerald", 1925, 4.75 };
Book frankenstein = { "Frankenstein", "Mary Shelley", 1823, 5.99 };
string flies = "Lord of the Flies";
int year = 1954;
}
gatsby-
gatsbyis aBook, not an instance variable ofBook. title-
titleis an instance variable ofBook. year-
yearis anintdeclared inmain, not an instance variable ofBook. price-
priceis an instance variable ofBook.
Q-2: Which of the following are instance variables of type Book?
struct Book {
string title, author;
int yearPublished;
double price;
};
int main() {
Book gatsby = { "The Great Gatsby", "F. Scott Fitzgerald", 1925, 4.75 };
Book frankenstein = { "Frankenstein", "Mary Shelley", 1823, 5.99 };
string flies = "Lord of the Flies";
int year = 1954;
}
To Kill a Mockingbird by Harper Lee (1960), $9.25
-
Take a closer look at
main. Was the price of the book modified? “To Kill a Mockingbird” by Harper Lee (1960), $9.25
-
Take a closer look at
main. Was the price of the book modified? “To Kill a Mockingbird” by Harper Lee (1960), $7.19
-
The price was changed from $9.25 to $7.19
To Kill a Mockingbirdby Harper Lee (1960), $7.19
-
The
\are escape characters. Used in this context, they allow us to print quotation marks.
Q-3: What is the output of the code below?
struct Book {
string title, author;
int yearPublished;
double price;
};
void printBook (Book& b) {
cout << "\"" << b.title << "\" by " << b.author << " (" << b.yearPublished << "), $" << b.price << endl;
}
int main() {
Book mockingbird = { "To Kill a Mockingbird", "Harper Lee", 1960, 9.25 };
double discountedPrice = 7.19;
mockingbird.price = discountedPrice;
printBook (mockingbird);
}
Pure function
-
printBooktakes aBookas an object but it doesn’t modify it. Modifier function
-
Does
printBookmodify theBookobject? Fill-in function
-
printBooktakes one parameter, and its parameter is not an emptyBookobject. Fruitful function
-
printBookdoes not return anything.
Q-4: What kind of function is printBook?
struct Book {
string title, author;
int yearPublished;
double price;
};
void printBook (Book& b) {
cout << "\"" << b.title << "\" by " << b.author << " (" << b.yearPublished << "), $" << b.price << endl;
}
int main() {
Book dracula = { "Dracula", "Bram Stoker", 1897, 3.95 };
printBook (dracula);
}
The
structdefinition is missing a semicolon at the end.-
It’s a common mistake to forget the semicolon at the end of
structdefinitions. We are not allowed to pass in a
Bookobject by reference inprintBook.-
We are allowed to do this. It’s usually a good idea to pass structures by reference since it won’t make copies of the structures, thus saving memory space.
The keyword
constneeds to be removed in the function definition forapplyDiscount.-
Since the
applyDiscountfunction modifies theBookpassed into it, we don’t it to beconst. There are no errors with the code.
-
There are a couple errors. Can you find them?
Q-5: What is wrong with the code below?
struct Book {
string title, author;
int yearPublished;
double price;
}
void printBook (Book& b) {
cout << "\"" << b.title << "\" by " << b.author << " (" << b.yearPublished << "), $" << b.price << endl;
}
void applyDiscount (const Book& b, double discount) {
b.price -= discount;
}
int main() {
Book godfather = { "The Godfather", "Mario Puzo", 1969, 10.90 };
applyDiscount (godfather, 5.40);
printBook (godfather);
}
Pure function
-
Does
applyDiscountmodify theBookobject? Modifier function
-
applyDiscountmodifies theBookobject by updating the price. Fill-in function
-
applyDiscountdoes not take an emptyBookobject as a parameter. Fruitful function
-
applyDiscountdoes not return anything.
Q-6: What kind of function is applyDiscount?
struct Book {
string title, author;
int yearPublished;
double price;
};
void printBook (Book& b) {
cout << "\"" << b.title << "\" by " << b.author << " (" << b.yearPublished << "), $" << b.price << endl;
}
void applyDiscount (Book& b, double discount) {
b.price -= discount;
}
int main() {
Book godfather = { "The Godfather", "Mario Puzo", 1969, 10.90 };
applyDiscount (godfather, 5.40);
printBook (godfather);
}
(4.5, 4.25, 5.65)
-
Look at the function declaration of
midpointcarefully. (3.0, 5.0, 2.0)
-
Look at the function declaration of
midpointcarefully. (9.0, 8.5, 11.3)
-
Look at the function declaration of
midpointcarefully. (0, 0, 0)
-
The last parameter in
midpointis not passed by reference, so a copy is made and changes are made to the copy, not the original.
Q-7: What is the output of the code below?
struct Point3D {
double x, y, z;
};
void printPoint3D (const Point3D& p) {
cout << "(" << p.x << ", " << p.y << ", " << p.z << ")" << endl;
}
void midpoint (const Point3D& p1, const Point3D& p2, Point3D p3) {
p3.x = (p1.x + p2.x) / 2;
p3.y = (p1.y + p2.y) / 2;
p3.z = (p1.z + p2.z) / 2;
}
int main() {
Point3D p1 = { 3.0, 5.0, 2.0 };
Point3D p2 = { 6.0, 3.5, 9.3 };
Point3D p3 = { 0.0, 0.0, 0.0 };
midpoint (p1, p2, p3);
printPoint3D (p3);
}
Pure function
-
Does
midpointmodify aPoint3Dobject? Modifier function
-
midpointmodifies the lastPoint3Dobject. Fill-in function
-
midpointtakes an “empty” thirdPoint3Dand fills it with the average of the other twoPoint3Dobjects. Fruitful function
-
midpointdoes not return anything.
Q-8: What kind of function is midpoint?
struct Point3D {
double x, y, z;
};
void printPoint3D (const Point3D& p) {
cout << "(" << p.x << ", " << p.y << ", " << p.z << ")" << endl;
}
void midpoint (const Point3D& p1, const Point3D& p2, Point3D& p3) {
p3.x = (p1.x + p2.x) / 2;
p3.y = (p1.y + p2.y) / 2;
p3.z = (p1.z + p2.z) / 2;
}
int main() {
Point3D p1 = { 3.0, 5.0, 2.0 };
Point3D p2 = { 6.0, 3.5, 9.3 };
Point3D p3 = { 0.0, 0.0, 0.0 };
midpoint (p1, p2, p3);
printPoint3D (p3);
}
(11.3, 4.5, 2.9)
-
Take a closer look at the implementation of
reflectXYPlane. (11.3, 4.5, -2.9)
-
The point is reflected across the XY plane, so the z value is inverted.
(-11.3, -4.5, 2.9)
-
Take a closer look at the implementation of
reflectXYPlane. (5.65, 2.25, 1.45)
-
Take a closer look at the implementation of
reflectXYPlane.
Q-9: What is the output of the code below?
struct Point3D {
double x, y, z;
};
void printPoint3D (const Point3D& p) {
cout << "(" << p.x << ", " << p.y << ", " << p.z << ")" << endl;
}
void midpoint (const Point3D& p1, const Point3D& p2, Point3D& p3) {
p3.x = (p1.x + p2.x) / 2;
p3.y = (p1.y + p2.y) / 2;
p3.z = (p1.z + p2.z) / 2;
}
Point3D reflectXYPlane(const Point3D& p) {
Point3D flipped = p;
flipped.z = -flipped.z;
return flipped;
}
int main() {
Point3D p = { 11.3, 4.5, 2.9 };
Point3D pReflected = reflectXYPlane (p);
printPoint3D (pReflected);
}
(5.45, 4, 1.65)
-
Take a closer look at the implementation of
reflectXYPlane. (5.45, 4, 8.35)
-
Check the arguments passed into
midpoint. (7.0, 3.5, -6.7)
-
Take a closer look at which point is being printed.
(4.5, 2.25, 3.35)
-
Check the arguments passed into
midpoint.
Q-10: What is the output of the code below?
struct Point3D {
double x, y, z;
};
void printPoint3D (const Point3D& p) {
cout << "(" << p.x << ", " << p.y << ", " << p.z << ")" << endl;
}
void midpoint (const Point3D& p1, const Point3D& p2, Point3D& p3) {
p3.x = (p1.x + p2.x) / 2;
p3.y = (p1.y + p2.y) / 2;
p3.z = (p1.z + p2.z) / 2;
}
Point3D reflectXYPlane(const Point3D& p) {
Point3D flipped = p;
flipped.z = -flipped.z;
return flipped;
}
int main() {
Point3D p1 = { 7.0, 3.5, 6.7 };
Point3D p2 = { 2.0, 1.0, 0.0 };
Point3D p3 = { 3.9, 4.5, 10.0 };
Point3D p4 = reflectXYPlane (p1);
midpoint (p4, p3, p2);
printPoint3D (p2);
}