9.12. Multiple Choice ExercisesΒΆ

Q1

Which of the following are variables of type book?

struct book {
  string title, author;
  int year_published;
  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;
}

Q2

Which of the following are instance variables of type book?

struct book {
  string title, author;
  int year_published;
  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;
}

Q3

What is the output of the code below?

struct book {
  string title, author;
  int year_published;
  double price;
};

void print_book (book& b) {
  cout << '"' << b.title << "\" by " << b.author << " (" << b.year_published << "), $" << b.price << '\n';
}

int main() {
  book mockingbird = { "To Kill a Mockingbird", "Harper Lee", 1960, 9.25 };
  double discounted_price = 7.19;
  b.price = discounted_price;
  print_book (mockingbird);
}

Q4

What kind of function is print_book?

struct book {
  string title, author;
  int year_published;
  double price;
};

void print_book (book& b) {
  cout << '"' << b.title << "\" by " << b.author << " (" << b.year_published << "), $" << b.price << '\n';
}

int main() {
  book dracula = { "Dracula", "Bram Stoker", 1897, 3.95 };
  print_book (dracula);
}

Q5

What is wrong with the code below?

struct book {
  string title, author;
  int year_published;
  double price;
}

void print_book (book& b) {
  cout << '"' << b.title << "\" by " << b.author << " (" << b.year_published << "), $" << b.price << '\n';
}

void apply_discount (const book& b, double discount) {
  b.price -= discount;
}

int main() {
  book godfather = { "The Godfather", "Mario Puzo", 1969, 10.90 };
  apply_discount (godfather, 5.40);
  print_book (godfather);
}

Q6

What kind of function is apply_discount?

struct book {
  string title, author;
  int year_published;
  double price;
};

void print_book (book& b) {
  cout << '"' << b.title << "\" by " << b.author << " (" << b.year_published << "), $" << b.price << '\n';
}

void apply_discount (book& b, double discount) {
  b.price -= discount;
}

int main() {
  book godfather = { "The Godfather", "Mario Puzo", 1969, 10.90 };
  apply_discount (godfather, 5.40);
  print_book (godfather);
}

Q7

What is the output of the code below?

struct point_3d {
  double x, y, z;
};

void print_point_3d (const point_3d& p) {
  cout << '(' << p.x << ", " << p.y << ", " << p.z << ')' << '\n';
}

void midpoint (const point_3d& p1, const point_3d& p2, point_3d p3) {
  p3.x = (p1.x + p2.x) / 2;
  p3.y = (p1.y + p2.y) / 2;
  p3.z = (p1.z + p2.z) / 2;
}

int main() {
  point_3d p1 = { 3.0, 5.0, 2.0 };
  point_3d p2 = { 6.0, 3.5, 9.3 };
  point_3d p3 = { 0.0, 0.0, 0.0 };
  midpoint (p1, p2, p3);
  print_point_3d (p3);
}

Q8

What kind of function is midpoint?

struct point_3d {
  double x, y, z;
};

void print_point_3d (const point_3d& p) {
  cout << '(' << p.x << ", " << p.y << ", " << p.z << ')' << '\n';
}

void midpoint (const point_3d& p1, const point_3d& p2, point_3d& p3) {
  p3.x = (p1.x + p2.x) / 2;
  p3.y = (p1.y + p2.y) / 2;
  p3.z = (p1.z + p2.z) / 2;
}

int main() {
  point_3d p1 = { 3.0, 5.0, 2.0 };
  point_3d p2 = { 6.0, 3.5, 9.3 };
  point_3d p3 = { 0.0, 0.0, 0.0 };
  midpoint (p1, p2, p3);
  print_point_3d (p3);
}

Q9

What is the output of the code below?

struct point_3d {
  double x, y, z;
};

void print_point_3d (const point_3d& p) {
  cout << '(' << p.x << ", " << p.y << ", " << p.z << ')' << '\n';
}

void midpoint (const point_3d& p1, const point_3d& p2, point_3d& p3) {
  p3.x = (p1.x + p2.x) / 2;
  p3.y = (p1.y + p2.y) / 2;
  p3.z = (p1.z + p2.z) / 2;
}

point_3d reflect_xy_plane(const point_3d& p) {
  point_3d flipped = p;
  flipped.z = -flipped.z;
  return flipped;
}

int main() {
  point_3d p = { 11.3, 4.5, 2.9 };
  point_3d p_reflected = reflect_xy_plane (p);
  print_point_3d (p_reflected);
}

Q10

What is the output of the code below?

struct point_3d {
  double x, y, z;
};

void print_point_3d (const point_3d& p) {
  cout << '(' << p.x << ", " << p.y << ", " << p.z << ')' << '\n';
}

void midpoint (const point_3d& p1, const point_3d& p2, point_3d& p3) {
  p3.x = (p1.x + p2.x) / 2;
  p3.y = (p1.y + p2.y) / 2;
  p3.z = (p1.z + p2.z) / 2;
}

point_3d reflect_xy_plane(const point_3d& p) {
  point_3d flipped = p;
  flipped.z = -flipped.z;
  return flipped;
}

int main() {
  point_3d p1 = { 7.0, 3.5, 6.7 };
  point_3d p2 = { 2.0, 1.0, 0.0 };
  point_3d p3 = { 3.9, 4.5, 10.0 };
  point_3d p4 = reflect_xy_plane (p1);
  midpoint (p4, p3, p2);
  print_point_3d (p2);
}