COSC quiz 6a 1

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/99

encourage image

There are no tags or description

Looks like no tags are added yet.

100 Terms

1
New cards
Which conditional structure is used in mapChoiceToItem() to map a choice number to an item name?
A nested ternary conditional operator, using condition ? valueIfTrue : valueIfFalse expressions.
2
New cards
Why is a nested ternary conditional operator used in mapChoiceToItem()?
It selects the item name corresponding to the choice number and returns one string value.
3
New cards
Which function maps a menu choice to an item name?
mapChoiceToItem(int choice).
4
New cards
Which function determines whether an item is taxable?
isTaxable(int choice).
5
New cards
Which function prints a formatted invoice line?
printInvoiceLine(string label, double amount).
6
New cards
Which function gets an integer from the user?
getIntInput(string prompt).
7
New cards
Which function gets a decimal number from the user?
getDoubleInput(string prompt).
8
New cards
How many required user-defined functions are declared in the prototypes?
Five: getIntInput, getDoubleInput, mapChoiceToItem, isTaxable, and printInvoiceLine.
9
New cards
What is a function prototype?
A declaration specifying a function's name, return type, and parameter types before its definition is used.
10
New cards
Which line calls mapChoiceToItem() inside the menu loop?
cout << i << "." << mapChoiceToItem(i) << ":" << endl;
11
New cards
Why does the menu use a for loop?
To display all ten menu options by repeatedly changing i from MIN_CHOICE to MAX_CHOICE.
12
New cards
How many times does the menu for loop execute with the given constants?
Ten times, because i runs from 1 through 10 inclusive.
13
New cards
What is the initial value of i in the menu loop?
MIN_CHOICE, which is 1.
14
New cards
What causes the menu loop to stop?
The condition i <= MAX_CHOICE becomes false after i increases to 11.
15
New cards
Which loop validates the user's menu choice?
A while loop.
16
New cards
Why does the program use a while loop to validate itemChoice?
It repeatedly asks for another choice while the entered number is less than 1 or greater than 10.
17
New cards
What does itemChoice < MIN_CHOICE || itemChoice > MAX_CHOICE mean?
The choice is invalid if it is below 1 OR above 10.
18
New cards
What does || mean in the validation condition?
Logical OR. The condition is true if either comparison is true.
19
New cards
What happens if the user enters 0 for the menu choice?
The program displays an invalid-choice message and prompts the user again.
20
New cards
What is the minimum valid menu choice?
1, stored in MIN_CHOICE.
21
New cards
What is the maximum valid menu choice?
10, stored in MAX_CHOICE.
22
New cards
Why are MIN_CHOICE and MAX_CHOICE named constants?
They give meaningful names to the menu boundaries and make them easier to maintain.
23
New cards
What is the value of TAX_RATE?
0.08, representing 8% tax.
24
New cards
What is the value of DISCOUNT_RATE?
0.05, representing a 5% senior discount.
25
New cards
What is the value of SENIOR_AGE?
60.
26
New cards
What does const mean in the global constants?
The values cannot be modified through those const declarations after initialization.
27
New cards
Where are the global named constants declared?
Outside all functions, before main().
28
New cards
What is a global variable or constant?
A name declared outside all functions that can be accessed within its scope, subject to visibility rules.
29
New cards
What does cin >> value do?
Reads a value from standard input and stores it in value.
30
New cards
What does cout << prompt do inside getIntInput()?
Displays the prompt so the user knows what to enter.
31
New cards
What does return value; do in getIntInput()?
Sends the integer stored in value back to the calling function.
32
New cards
What is the parameter in getIntInput(string prompt)?
string prompt.
33
New cards
What is the parameter in getDoubleInput(string prompt)?
string prompt.
34
New cards
How many parameters does mapChoiceToItem(int choice) have?
One parameter: int choice.
35
New cards
What type of value does mapChoiceToItem() return?
A string.
36
New cards
What type of value does isTaxable() return?
A bool, meaning true or false.
37
New cards
What type of value does printInvoiceLine() return?
void; it does not return a value.
38
New cards
What is the difference between a parameter and an argument?
A parameter appears in a function header; an argument is the value supplied when the function is called.
39
New cards
What does the ternary operator condition ? valueIfTrue : valueIfFalse do?
It evaluates the condition and produces one of two expressions depending on whether the condition is true or false.
40
New cards
How does the program calculate tax?
It uses isTaxable(itemChoice) ? (price * TAX_RATE) : 0.0.
41
New cards
Why is a ternary operator used to calculate tax?
It selects either the calculated tax or 0.0 based on whether the selected item is taxable.
42
New cards
What does the ! operator mean in isTaxable()?
Logical NOT. It reverses the result of the parenthesized Boolean expression.
43
New cards
What does || mean inside isTaxable()?
Logical OR. It checks whether the choice matches any of the listed non-taxable choices.
44
New cards
Select all that apply: Which choices are coded as non-taxable?
1 (Bread), 2 (Milk), 4 (Eggs), and 6 (Juice).
45
New cards
Select all that apply: Which choices are coded as taxable?
3 (Soap), 5 (Deodorant), 7 (Chips), 8 (Forks), 9 (Spoons), and 10 (Cups).
46
New cards
What does double tax = isTaxable(itemChoice) ? (price * TAX_RATE) : 0.0; calculate?
It calculates 8% of price for taxable items and assigns 0.0 tax for non-taxable items.
47
New cards
How does the program calculate the senior discount?
It uses (age >= SENIOR_AGE) ? -(price * DISCOUNT_RATE) : 0.0.
48
New cards
What happens when age is exactly 60?
The discount calculation applies because age >= 60 is true.
49
New cards
What happens when age is 59?
The discount is 0.0 because age >= 60 is false.
50
New cards
Why is there a minus sign before (price * DISCOUNT_RATE)?
The discount is stored as a negative amount so it is subtracted when added to the total.
51
New cards
What is the discount rate?
5%, represented by 0.05.
52
New cards
What does double total = price + tax + discountAmount; do?
Calculates the total by adding the price, tax, and discount amount, which is negative for eligible seniors.
53
New cards
Does the program ask the user to enter the tax rate?
No. TAX_RATE is a global named constant.
54
New cards
Does the program ask the user to enter the discount rate?
No. DISCOUNT_RATE is a global named constant.
55
New cards
Does the program ask the user to enter the item name?
No. The program maps the validated menu choice to the item name.
56
New cards
Does the program ask the user to enter the item price?
Yes. It prompts for the price using getDoubleInput().
57
New cards
Does the program ask the user to enter their age?
Yes. It prompts for the age using getIntInput().
58
New cards
Select all that apply: Which information does the user enter?
The menu choice, item price, and age.
59
New cards
Select all that apply: Which values does the program calculate or determine?
The item name, tax amount, discount amount, and total.
60
New cards
Select all that apply: Which values are defined by named constants?
Tax rate, discount rate, senior age, minimum choice, and maximum choice.
61
New cards
Does the program request an output filename?
No. It displays the invoice on the screen using cout.
62
New cards
What does cout << "\tInvoice" << endl; display?
It displays the word Invoice with a tab before it, then moves to a new line.
63
New cards
What does \t represent in a C++ string?
A tab character.
64
New cards
What does printInvoiceLine(itemName + ":", price); do?
It prints the item name followed by a colon and the price.
65
New cards
What does printInvoiceLine("Tax:", tax); do?
It prints the tax label and the calculated tax amount.
66
New cards
When is the Discount line printed?
Only when age >= SENIOR_AGE is true.
67
New cards
Why does the program use if (age >= SENIOR_AGE) around the discount line?
To display the discount line only for customers who qualify for the senior discount.
68
New cards
Is the discount calculated for a customer younger than 60?
The discount amount is set to 0.0, and the Discount line is not printed.
69
New cards
Does the program always print a Total line?
Yes. printInvoiceLine("Total:", total) is called regardless of age.
70
New cards
What is the purpose of printInvoiceLine()?
It prints a label and amount with two decimal places and a chosen alignment pattern.
71
New cards
What does label.length() >= 8 check?
Whether the label contains at least eight characters.
72
New cards
Why does printInvoiceLine() use an if-else statement?
It chooses between two tab layouts based on the label length.
73
New cards
What happens if label.length() >= 8 is true?
The function prints the label, one tab, a dollar sign, the amount, and a newline.
74
New cards
What happens if label.length() >= 8 is false?
The function prints the label, two tabs, a dollar sign, the amount, and a newline.
75
New cards
Does printInvoiceLine() use setw()?
No. It uses tab characters to separate the label from the dollar amount.
76
New cards
Does printInvoiceLine() use tabs to format the invoice?
Yes. It uses \t and sometimes \t\t depending on the label length.
77
New cards
Does printInvoiceLine() use setprecision()?
Yes. It uses setprecision(2) together with fixed.
78
New cards
Does setprecision(2) always mean two decimal places?
No. Without fixed, it normally specifies significant digits for floating-point output.
79
New cards
What does cout << fixed << setprecision(2); do in printInvoiceLine()?
It configures the amount to display with two digits after the decimal point.
80
New cards
Select all that apply: Which formatting techniques are used in printInvoiceLine()?
Tabs, fixed, and setprecision(2). It does not use setw().
81
New cards
Select all that apply: Which conditional structures appear in the invoice program?
A for loop, a while loop, if statements, an if-else statement, and ternary conditional operators.
82
New cards
Does the invoice program use a switch statement?
No. It uses nested ternary expressions in mapChoiceToItem() and a Boolean expression in isTaxable().
83
New cards
Does the invoice program use a for loop?
Yes. It uses a for loop to display the ten menu options.
84
New cards
Does the invoice program use a while loop?
Yes. It uses a while loop to repeat the menu prompt when the choice is invalid.
85
New cards
Does the invoice program use an if-else statement?
Yes. printInvoiceLine() uses if-else to choose between one and two tab characters.
86
New cards
What does return 0; in main() mean?
It indicates that main() has finished successfully.
87
New cards
Why does the program call mapChoiceToItem() twice in different places?
Once to display each menu item and again to determine the selected item's name.
88
New cards
What does int i = MIN_CHOICE; do in the for loop?
It initializes the loop counter to 1.
89
New cards
What does i <= MAX_CHOICE mean?
The loop continues while i is less than or equal to 10.
90
New cards
What is the purpose of the function prototypes at the beginning?
They tell the compiler about the five functions before main() calls them.
91
New cards
Why are the function definitions written after main()?
The prototypes allow main() to call the functions before their full definitions appear later in the file.
92
New cards
What is a potential issue in the isTaxable() function?
The statement return choice; is unreachable because the preceding return statement exits the function. It is also unnecessary and does not match the function's bool return type.
93
New cards

To map a menu choice to an item name, the program uses:

return (choice == 1) ? "Bread":
       (choice == 2) ? "Milk":
       (choice == 3) ? "Soap":
       "Unknown";

Why is a ternary conditional operator used?


Because it selects and returns one string value based on the conditions.

94
New cards

Which information does the user enter when running this program?

A. The item price
B. The output filename
C. The user's age
D. The tax rate
E. The menu choice
F. The calculated total

A, C, E

95
New cards

To format the invoice, the program uses:

A. setw() with a number in parentheses to set column width
B. setw() with a named constant to set column width
C. Tabs to separate the label and amount
D. setprecision() with a number in parentheses
E. setprecision() together with fixed to specify decimal places
F. setprecision() together with fixed to specify significant digits


A, B, C, D, E

96
New cards

What does the following statement do?

double tax = isTaxable(itemChoice) ? (price * TAX_RATE) : 0.0;


It calculates 8% tax if the item is taxable; otherwise, it assigns zero.

97
New cards

What happens if the user's age is 59?

double discountAmount =
    (age >= SENIOR_AGE) ? -(price * DISCOUNT_RATE) : 0.0;


The discount amount is zero.

98
New cards

What is the purpose of this loop?

for (int i = MIN_CHOICE; i <= MAX_CHOICE; i++) {
    cout << i << "." << mapChoiceToItem(i) << ":" << endl;
}


To display the ten menu options.

99
New cards

Why does the program use this condition?

while (itemChoice < MIN_CHOICE || itemChoice > MAX_CHOICE)


To repeat the prompt while the choice is outside the valid range.

100
New cards

Which line calls the function that formats and prints the tax amount?

A. void printInvoiceLine(string label, double amount);
B. printInvoiceLine("Tax:", tax);
C. double tax = isTaxable(itemChoice) ? (price * TAX_RATE) : 0.0;
D. void printInvoiceLine(string label, double amount)

printInvoiceLine("Tax:", tax);