Exam Questions Ch4 Making Decisions - Test Bank | C++ Control Structures 9e by Tony Gaddis. DOCX document preview.
Starting Out with C++ from Control Structures to Objects, 9e (Gaddis)
Chapter 4 Making Decisions
TRUE/FALSE
1. If the expression on the left side of the following is true, the expression on the right side will not be checked.
(a > = b) || (c == d)
2. If the expression on the left side of the following is false, the expression on the right side will not be checked.
(a > = b) && (c == d)
3. The default section is required in a switch statement.
4. An expression that has any value other than 0 is considered true by an if statement.
5. Both of the following if statements perform the same operation.
1. if (sales > 10000)
commissionRate = 0.15;
2. if (sales > 10000) commissionRate = 0.15;
6. You should be careful when using the equality operator to compare floating point values because of potential round-off errors.
7. As a rule of style, when writing an if statement you should indent the conditionally-executed statements.
8. The following code correctly determines whether x contains a value in the range of 0 through 100, inclusive.
if (x > 0 && <= 100)
9. The value of result in the following expression will be 0 if x has the value of 12.
result = x > 100 ? 0 : 1;
10. The conditional operator takes two operands.
MULTIPLE CHOICE
1. Relational operators allow you to __________ numbers.
a. | add |
b. | multiply |
c. | compare |
d. | average |
e. | None of these |
2. After the following code executes, what is the value of my_value if the user enters 0?
cin >> my_value;
if (my_value > 5)
my_value = my_value + 5;
else if (my_value > 2)
my_value = my_value + 10;
else
my_value = my_value + 15;
a. | 15 |
b. | 10 |
c. | 25 |
d. | 0 |
e. | 5 |
3. After the following code executes, what is the output if user enters 0?
int x = -1;
cout << "Enter a 0 or 1: ";
cin >> x;
if (c)
cout << "true" << endl;
else
cout << "false" << endl;
a. | nothing will be displayed |
b. | false |
c. | x |
d. | true |
e. | 0 |
4. What is assigned to the variable result given the statement below with the following assumptions: x = 10, y = 7, and x, result, and y are all int variables.
result = x >= y;
a. | 10 |
b. | 7 |
c. | x >= y |
d. | 1 |
e. | 0 |
5. If you place a semicolon after the statement:
if (x < y)
a. | the code will not compile |
b. | the compiler will interpret the semicolon as a null statement |
c. | the if statement will always evaluate to false |
d. | All of these are true |
e. | None of these |
6. When a relational expression is false, it has the value
a. | 1 |
b. | 0 |
c. | 0, 1, or -1 |
d. | -1 |
e. | None of these |
7. What is the output of the following code segment?
int x = 5;
if (x = 2)
cout << "This is true!" << endl;
else
cout << "This is false!" << endl;
cout << "That's all, folks!" << endl;
a. | This is true! |
b. | This is false! |
c. | This is false! That's all, folks |
d. | This is true! That's all folks |
e. | This is true! This is false! That's all, folks! |
8. What is the output of the following code segment if the user enters 90 for the score?
cout << "Enter your test score: ";
cin >> test_score;
if (test_score < 60)
cout << "You failed the test." << endl;
if (test_score > 60)
cout << "You passed the test."
else
cout << "You need to study harder next time." << endl;
a. | You failed the test. |
b. | You passed the test. |
c. | You need to study harder next time. |
d. | You failed the test. You need to study harder next time. |
e. | You passed the test. You need to study harder next time. |
9. Whereas < is called a relational operator, x < y is called a(n)
a. | arithmetic operator |
b. | relative operator |
c. | relational expression |
d. | arithmetic expression |
e. | None of these |
10. The __________ is an equality (or comparison) operator.
a. | == |
b. | >= |
c. | != |
d. | = |
e. | None of these |
11. What is the output of the following code segment if the user enters 23?
int number;
cout << "Enter a number: ";
cin >> number;
if (number > 0)
cout << "Hi, there!" << endl;
else
cout << "Good-bye." << endl;
a. | Hi, there! Good-bye. |
b. | Hi, there! |
c. | Good-bye. |
d. | "Hi, there!" |
e. | nothing will output |
12. What will be displayed after the following statements execute?
int funny = 7, serious = 15;
funny = serious % 2;
if (funny != 1)
{
funny = 0;
serious = 0;
}
else if (funny == 2)
{
funny = 10;
serious = 10;
}
else
{
funny = 1;
serious = 1;
}
cout << funny << " " << serious << endl;
a. | 7 15 |
b. | 0 0 |
c. | 10 10 |
d. | 1 1 |
e. | None of these |
13. What is the value of donuts after the following statement executes?
int donuts = 10;
if (donuts != 10)
donuts = 0;
else
donuts += 2;
a. | 12 |
b. | 10 |
c. | 0 |
d. | 1 |
e. | None of these |
14. What is the value of donuts after the following statement executes?
int donuts = 10;
if (donuts = 1)
donuts = 0;
else
donuts += 2;
a. | 12 |
b. | 10 |
c. | 0 |
d. | 1 |
e. | None of these |
15. Which of the following expressions will determine whether x is less than or equal to y?
a. | x > y |
b. | x =< y |
c. | x >= y |
d. | x <= y |
e. | x == y and x < y |
16. What is the value of result after the following code executes?
int a = 60;
int b = 15;
int result = 10;
if (a = b)
result *= 2;
a. | 10 |
b. | 120 |
c. | 20 |
d. | 12 |
e. | code will not execute |
17. When an if statement is placed within the conditionally-executed code of another if statement, this is known as
a. | complexity |
b. | overloading |
c. | nesting |
d. | validation |
e. | None of these |
18. If you intend to place a block of statements within an if statement, you must place __________ around the block:
a. | parentheses ( ) |
b. | square brackets [ ] |
c. | angle brackets < > |
d. | curly braces { } |
e. | None of these |
19. A variable, usually a bool or an int, that signals when a condition exists is known as a(n)
a. | relational operator |
b. | arithmetic operator |
c. | logical operator |
d. | flag |
e. | None of these |
20. This operator represents the logical AND:
a. | ++ |
b. | || |
c. | && |
d. | @ |
e. | None of these |
21. This operator represents the logical OR:
a. | -- |
b. | || |
c. | && |
d. | # |
e. | None of these |
22. This operator takes an operand and reverses its truth or falsehood:
a. | !& |
b. | || |
c. | =! |
d. | ! |
e. | None of these |
23. What is the value of the following expression?
true && false
a. | true |
b. | false |
c. | -1 |
d. | +1 |
e. | None of these |
24. What is the value of the following expression?
true && true
a. | true |
b. | false |
c. | -1 |
d. | +1 |
e. | None of these |
25. What is the value of the following expression?
true || false
a. | true |
b. | false |
c. | -1 |
d. | +1 |
e. | None of these |
26. What is the value of the following expression?
true && !false
a. | true |
b. | false |
c. | -1 |
d. | +1 |
e. | None of these |
27. These operators connect two or more relational expressions into one, or reverse the logic of an expression.
a. | relational |
b. | logical |
c. | irrational |
d. | negation |
e. | None of these |
28. Which value can be entered to cause the following code segment to display the message "That number is acceptable"?
int number;
cin >> number;
if (number > 10 && number < 100)
cout << "That number is acceptable.\n";
else
cout << "That number is not acceptable.\n";
a. | 100 |
b. | 10 |
c. | 99 |
d. | 0 |
e. | all of these |
29. Which of the following is evaluated first, given the expression:
A && B || C && !D
a. | A && B |
b. | B || C |
c. | C && !D |
d. | !D |
30. What is the output of the following code?
int w = 98;
int x = 99;
int y = 0;
int z = 1;
if (x >= 99)
{
if (x < 99)
cout << y << endl;
else
cout << z << endl;
}
else
{
if (x == 99)
cout << x << endl;
else
cout << w << endl;
}
a. | 98 |
b. | 99 |
c. | 0 |
d. | 1 |
e. | None of these |
31. Which line in the following program will cause a compiler error?
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 int number = 5;
6 if (number >= 0 && <= 100)
7 cout << "passed.\n";
8 else
9 cout << "failed.\n";
10 return 0;
11 }
a. | line 3 |
b. | line 6 |
c. | line 7 |
d. | line 9 |
e. | None will cause an error |
32. Given that x = 2, y = 1, z = 0, what will the following cout statement display?
cout << "answer = " << (x || !y && z) << endl;
a. | answer = 0 |
b. | answer = 1 |
c. | answer = 2 |
d. | None of these |
33. What is the output of the following segment of code if the value 4 is input by the user?
int num;
int total = 0;
cout << "Enter a number from 1 to 10: ";
cin >> num;
switch (num)
{
case 1:
case 2: total = 5;
case 3: total = 10;
case 4: total = total + 3;
case 8: total = total + 6;
default: total = total + 4;
}
cout << total << endl;
a. | 0 |
b. | 3 |
c. | 13 |
d. | 23 |
e. | None of these |
34. This statement uses the value of a variable or expression to determine where the program will branch to.
a. | switch |
b. | select |
c. | association |
d. | scope |
e. | None of these |
35. Without this statement appearing in a switch construct, the program "falls through" all of the statements below the one with the matching case expression.
a. | break |
b. | exit |
c. | switch |
d. | scope |
e. | None of these |
36. The default section of a switch statement performs a similar task similar to the __________ portion of an if/else if statement.
a. | conditional |
b. | break |
c. | trailing else |
d. | All of these |
e. | None of these |
37. Which statement allows you to properly check the char variable code to determine whether it is equal to a C and then output This is a check?
a. | if code is equal to C cout << "This is a check\n"; |
b. | if (code = "C") cout << "This is a check\n"; |
c. | if (code == 'C') cout << "This is a check\n"; |
d. | if (code == C) cout << "This is a check" << endl; |
38. Given the following code segment, what is the output?
int x = 1, y = 1, z = 1;
y = y + z;
x = x + y;
cout << "result = " << (x < y ? y : x) << endl;
a. | result = 0 |
b. | result = 1 |
c. | result = 2 |
d. | result = 3 |
e. | there will be no output |
39. Given the if/else statement:
if (a < 5)
b = 12;
else
d = 30;
Which of the following performs the same operation?
a. | a < 5 ? b = 12 : d = 30; |
b. | b < 5 ? b = 12 : d = 30; |
c. | a >= 5 ? d = 30 : b = 12; |
d. | d = 30 ? b = 12 : a = 5; |
e. | None of these |
40. When a program lets the user know that an invalid choice has been made, this is known as:
a. | input validation |
b. | output correction |
c. | compiler criticism |
d. | output validation |
e. | None of these |
41. Input values should always be checked for
a. | an appropriate range |
b. | reasonableness |
c. | division by zero, if division is taking place |
d. | All of these |
e. | None of these |
MULTIPLE RESPONSE
1. Select all that apply. Given: x = 5, y = 6, z = 8. Which of the following are false?
1. x == 5;
2. x < (y + 2);
3. z <= 4;
4. y > (z - x);
5. z >= (y + x)
6. y <= 6
a. | 1 | d. | 4 |
b. | 2 | e. | 5 |
c. | 3 | f. | 6 |