Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Exercises 9.13 Multiple Choice Exercises
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 is a Book.
frankenstein is a Book.
flies is a string.
year is an int.
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;
}
gatsby is a Book, not an instance variable of Book.
title is an instance variable of Book.
year is an int declared in main, not an instance variable of Book.
price is an instance variable of Book.
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);
}
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.
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);
}
printBook takes a Book as an object but it doesnβt modify it.
Does printBook modify the Book object?
printBook takes one parameter, and its parameter is not an empty Book object.
printBook does not return anything.
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);
}
The
struct definition is missing a semicolon at the end.
Itβs a common mistake to forget the semicolon at the end of struct definitions.
We are not allowed to pass in a
Book object by reference in
printBook.
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
const needs to be removed in the function definition for
applyDiscount.
Since the applyDiscount function modifies the Book passed into it, we donβt it to be const .
There are no errors with the code.
There are a couple errors. Can you find them?
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);
}
Does applyDiscount modify the Book object?
applyDiscount modifies the Book object by updating the price.
applyDiscount does not take an empty Book object as a parameter.
applyDiscount does not return anything.
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);
}
Look at the function declaration of midpoint carefully.
Look at the function declaration of midpoint carefully.
Look at the function declaration of midpoint carefully.
The last parameter in midpoint is not passed by reference, so a copy is made and changes are made to the copy, not the original.
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);
}
Does midpoint modify a Point3D object?
midpoint modifies the last Point3D object.
midpoint takes an βemptyβ third Point3D and fills it with the average of the other two Point3D objects.
midpoint does not return anything.
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);
}
Take a closer look at the implementation of reflectXYPlane.
The point is reflected across the XY plane, so the z value is inverted.
Take a closer look at the implementation of reflectXYPlane.
Take a closer look at the implementation of reflectXYPlane.
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);
}
Take a closer look at the implementation of reflectXYPlane.
Check the arguments passed into midpoint.
Take a closer look at which point is being printed.
Check the arguments passed into midpoint.
You have attempted
of
activities on this page.