Question

# Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item...

1. Code Example 8-1
1. int count = 1;
2. int item_total = 0;
3. int item = 0;
4. while (count < 4) {
5.      cout << "Enter item cost: ";
6.      cin >> item;
7.      item_total += item;
8.      ++count;
9. }
10. int average_cost = round(item_total / count);
11. cout << "Total cost: " << item_total << "\nAverage cost: " << average_cost;

(Refer to Code Example 8-1.) If the user enters 5, 10, and 15 at the prompts, the output is:
Total cost: 30
Average cost: 7
Which line should be changed to fix this logic error?

 a. change line 1 to: int count = 0; b. change line 10 to: int average_cost = round(item_total / (count - 1)); c. change line 7 to: item_total += item - 1; d. change line 4 to: while (count <= 4) {

1.5 points

QUESTION 31

1. Given the following Product structure:
struct Product {
string name;
double price;
int quantity;
bool operator==(const Product& to_compare) {
return (name == to_compare.name && price == to_compare.price);
}

};
what statement would you use to test if two Product objects named p1 and p2 are equal?

 a. bool duplicate = (p1 operator== p2); b. bool duplicate = p1.==(p2); c. bool duplicate = p1.operator==(p2); d. bool duplicate = (p1 == p2);

1.5 points

QUESTION 32

1. When you use the Visual Studio or Xcode debugger, you start by setting a breakpoint

 a. on the definition for the function that you think is causing the bug b. on a comment line c. on the statement you believe is causing the bug d. on a statement before the statement you think is causing the bug

1.5 points

QUESTION 33

1. What are the values of the elements in the queue named products after the following code is executed?
queue<string> products;
products.push("hammer");
products.push("nails");
products.push("wrench");
products.pop();

 a. hammer, nails, wrench b. wrench, nails, hammer c. nails, wrench d. nails, hammer

1.5 points

QUESTION 34

1. When you code a member function for a structure, you must

 a. declare it within the structure declaration b. define it within the structure declaration c. both a and b

1.5 points

QUESTION 35

1. Code Example 8-2
1. /* This application displays a student's score after a 5-point curve */
2.
3. void display_info(string fname, string lname, int score) {
4.      cout << "Hello, " << fname << ' ' << Lname;
5.      cout << "Your score on this is " << score;
6.      score = score + 5;
7. }
8.
9. int main() {
10.     string first, last;
11.     int score = 0;
12.     cout << "first name: ";
13.     cin >> first;
14.     cout << "last name: ";
15.     cin >> last;
16.     cout << " score: ";

18.     display_info(first, last, score);
19. }

(Refer to Code Example 8-2.) Assuming the compile-time errors have been fixed, what is the logic error in this program?

 a. The arguments are listed in the wrong order. b. The curve is calculated after the score has been displayed. c. The display_info() function should be called before the input statements that start on line 11. d. There is no logic error in this program.

1.5 points

QUESTION 36

1. Code Example 9-1
struct Product {
string name;
double price;
int quantity;

};

(Refer to Code Example 9-1.) What happens if you try to compare two Product objects using the following code?
Product p1 = { "hammer", 14.99, 15 };
Product p2 = { "hammer", 16.29, 12 };
if (p1 == p2) {
cout << "The products are equal.";
}
else {
cout << "The products are not equal.";
}

 a. A compile-time error occurs because there is no equality operator for the Product structure. b. It displays a message that the products are not equal because they have different prices and quantities. c. It displays a message that the products are equal because the have the same name.

1.5 points

QUESTION 37

1. An enumeration contains enumerators that represent

 a. the variables for the enumeration b. the operators for the enumeration c. the functions for the enumeration d. the constants for the enumeration

1.5 points

QUESTION 38

1. What are the values of the elements in the stack named products after the following code is executed?
stack<string> products;
products.push("hammer");
products.push("nails");
products.push("wrench");
products.pop();

 a. nails, hammer b. hammer, nails, wrench c. wrench, nails, hammer d. nails, wrench

1.5 points

QUESTION 39

1. Which of the following containers is a fixed-size collection of elements that’s stored in contiguous memory?

 a. array b. deque c. list d. map

(30)
(b) change line 10 to: int average_cost = round(item_total / (count - 1));

(31)
struct Product {
string name;
double price;
int quantity;
bool operator==(const Product& to_compare) {
return (name == to_compare.name && price == to_compare.price);
}
};
(d)bool duplicate = (p1 == p2);

(32)(d)on a statement before the statement you think is causing the bug

(33)the values of the elements in the queue named products
(c)nails, wrench

(34)c.    both a and b

(35)d. There is no logic error in this program.

(36) a. A compile-time error occurs because there is no equality operator for the Product structure.

(37)    d. the constants for the enumeration

(38)a.    nails, hammer

(39) a. array