Chapter 4 (Quizes 1-3)
If x is 3 and y is 8, which of the following relational expressions are true? y <= x x != y y > x x <= y x >= y x < y x > y y >= x
x != y y > x x <= y x < y y >= x
If x = 20 and y = 30, which of the following conditions evaluate to true? (Choose all that apply.) !(x > 50 && y < 40) !(x < 5 || y < 10) !(x > 0 && y > 5) !(x > 50 || y < 40) !(x == 20 && y == 10) !(x < y && y == 30)
!(x == 20 && y == 10) !(x < 5 || y < 10) !(x > 50 && y < 40)
Which is the correct order of precedence for the logical operators &&, !, and ||?
!, &&, ||
If x = 10 and y = 20, which of the following conditions evaluate to true? (Choose all that apply.) (x < 20 && y == 20) || (x == 3) (x > 50 && y > 0) || (x == 10 && y == 10) (x == 3) && (y == 20 || x > 40) (x > 40) || (y == 20 && x < 25)
(x < 20 && y == 20) || (x == 3) (x > 40) || (y == 20 && x < 25)
Assume value is a bool. If x is 12 and y is 5, what value will be stored in value in the statement below? value = x < y;
0
If x = 25 and y = 20, what will be the value of z after the following code segment executes? if (x + y > 100) z = x + y; else if (x + y > 75) z = x - y; else if (x + y > 50) z = x * y; else z = 0;
0
If x = 5 what will be the value of the variable y after the following conditional expression executes? y = x < 10 ? 0 : 1;
0
What is the correct answer for Checkpoint 4.16 on p. 180?
1 1
If x = 20 and y = 5, what will be the value of z after the following code segment executes? z = 10; if (x - y < 5) { if (x + y > 15) { z = z + x; } else { z = z + x; } } else { z = z + 3; } z = z - 3;
10
If x = 10 and y = 5, what will be the value of z after the following code segment executes? if (x + y > 20) z = x; else z = 3 * y;
15
If x = 50 and y = 50, what will be the value of z after the following code segment executes? z = 20; if (x - y < 0) { if (x + y > 15) { z = z - 5; } else { z = z + 10; } } else { if (x == y) { z = z + 5; } else { z = z - 2; } } z = z - 10;
15
For Program 4-27 on p. 208, if the user enters 3 for choice on line 31, and later enters 5 for months, what value will be stored in the variable charges?
150.0
If x = 5 and y = 15, what will be the value of z after the following code segment executes? z = 8; if (y - x < 5) { if (x + y > 15) { x = x + 10; z = z + x; } else { x = x + 20; z = z + x; } } z = z + 10;
18
For Checkpoint 4.17 on p. 180, if the user enters 4 for numBooks, what value will be stored in numCoupons?
2
For Program 4-26 on p. 206, if the user enters 'b' for feedGrade on line 14, what will the program display?
20 cents per pound.
If x = 5 and y = 2, what will be the value of z after the following code segment executes? z = 12; if (x - y > 0) { if (x > y) { x += 5; z += x; } }
22
For Checkpoint 4.17 on p. 180, if the user enters 10 for numBooks, what value will be stored in numCoupons?
3
If x = 20 and y = 10, what will be the value of z after the following code segment executes? z = 4; if (y - x < 2) { if (x + y > 30) { x = x + 1; z = z + x; } else { x = x + 5; z = z + x; } } z = z + 10;
39
If x = 10 and y = 12, what will be the value of z after the following code segment executes? z = 35; if (y - x >= 2) { if (x + y > 22) { x += 1; z += x; } } z += 10;
45
Assume that the variables x and MAX_SPEED are defined as follows: const int MAX_SPEED = 200; int x; Which of the following are valid expressions that can follow the word case in a switch statement? (Choose all that apply.) 50 'A' MAX_SPEED 'b' x x < 0 y == 20
50 'A' MAX_SPEED 'b'
For Program 4-18 on p. 191, if the user enters 2 for choice (line 32) and later enters 3 for months, what value will be stored in the variable charges?
60.0
If x = 20 and y = 50, what will be the value of z after the following code segment executes? if (x + y == 70) { x += x; z = x + y; } else { y += y; z = x - y; }
90
Which of the relational expressions in CheckPoint 4.22 on p. 198 will evaluate to true?
A, C, and F
In Program 4-2 on p. 156, if the user enters 93 98 95, what is the last line that the program will display on the screen?
Congratulations! That's a high score!
T/F: A flag is typically a bool variable such that when the flag variable is set to false, it indicates that the condition exist.
False
T/F: In C++ the value true is represented by the number 0 and false is represented by the number 1.
False
T/F: The basic structure of a switch statement is shown at the bottom of p. 202. The default section is not optional.
False
Which of the following statements are true? If x < y and x >= z, then z < y If y > x and y < z, then x < z. If y < z and y < x, then x < z. If z != x and z != y, then y != x If x <= y and y <= z, then x < z If x <= y and y <= z, then x <= z
If y > x and y < z, then x < z. If x < y and x >= z, then z < y If x <= y and y <= z, then x <= z
In Program 4-14 on p. 179, if the user enters -1 for testScore in line 19, what will the program display?
Invalid test score.
For Program 4-27 on p. 208, if the user enters 4 for choice on line 31, what will the program display?
Program ending.
For Program 4-20 on p. 195, if the user enters $ for ch on line 12, what will the program display?
That is not a digit or a letter.
What is the correct answer for Checkpoint 4.29 on p. 210?
That is serious.
For Program 4-22 on p. 200, if the user enters 6 for hours in line 17, what will the program display?
The charges are $300.00
T/F: Assume that found is a bool variable. The following two statements are equivalent: if (found) cout << "The search string has been found." if (found == true) cout << "The search string has been found."
True
T/F: Assume that found is an int variable. The following two statements are equivalent: if (found) cout << "The search string has been found." if (found == 1) cout << "The search string has been found."
True
T/F: In an if/else if statement, the starting if clause, the else if clauses, and the trailing else clause are all aligned, and the conditionally executed statements are indented.
True
T/F: The == operator determines if a variable is equal to another value, but the = operator is used in an assignment statement.
True
T/F: The case statements in a switch statement show the program where to start executing in the block and the break statements show the program where to stop.
True
T/F: The expression following the word case in a switch statement must be an integer literal or constant. It cannot be a variable, and it cannot be an expression such as x < 22 or n == 50.
True
T/F: The statement below is an example of a conditional expression. x < 0 ? y = 10 : z = 20;
True
For Program 4-24 on p. 204, if the user enters 'H' for choice on line 11, what will the program display?
You did not enter A, B, or C!
For Program 4-15 on p. 183, if the user enters Y for employed (line 15) and Y for recentGrad (line 19), what will the program display?
You qualify for the special interest rate.
In Program 4-2 on p. 156, if the user enters 85 94 99, what is the last line that the program will display on the screen?
Your average is 92.7
In Program 4-13 on p. 177, if the user enters 80 for testScore in line 19, what will the program display?
Your grade is B.
Look at Program 4-12 on p. 174. If the user enters 78 for testScore, what will the program display?
Your grade is C.
What is the advantage of uses the if/else if statement instead of a series of nested if/else statements. a. The code for nested if/else statements can grow complex and become difficult to read. b. The logic of an if/else if statement is usually easier to follow than that of a long series of nested if/else statements. c. A long series of nested if/else statements can become too long to be displayed on the computer screen without horizontal scrolling. d. Long statements in a series of nested if/else statements tend to wrap around when printed on paper. e. all of these
all of these
Without the ________ statements, the program would execute all of the lines in a switch statement from the matching case statement to the end of the block.
break
Assume that a, b, and c are int variables. If a = 3, b = 5, and c = 7, which of the following cout statements will display the number 0? cout << (2 * a < c) << endl; cout << (a == c) << endl; cout << (a * b != b * c) << endl; cout << (a - b != b - a - 4) << endl;
cout << (a == c) << endl; cout << (a - b != b - a - 4) << endl;
Refer to CheckPoint 4.18 exercise on p. 189. Which combinations of the values true and false evaluate to true? (Choose all that Apply) false || true true || true true && true true && false ! true false || false false && true false && false true || false !false
false || true true || true true && true true || false !false
Which of the following statements will assign the value of 0.5 to discount_rate if the variable price is greater than 800? if (price > 800) discountrate = 0.5; if (price > 800) discount_rate = 0.5 if (price > 800) discountrate = 0.5 if (price > 800) discount_rate = 0.5;
if (price > 800) discount_rate = 0.5;
Assume x = 4, and y = 6. Which of the following if statements will result in the message "Condition is true" being printed on the screen? if (x) cout << "Condition is true." if (x - y) cout << "Condition is true." if (x * y) cout << "Condition is true." if (y - 6) cout << "Condition is true." if (x - y + 2) cout << "Condition is true."
if (x * y) cout << "Condition is true." if (x - y) cout << "Condition is true." if (x) cout << "Condition is true."
Which of the following statements will assign the value of z + y to x if x is equal to 45? if (x = 45) x = z + y; if (x == 45) x = z + y; if (x == 45) x = z + y
if (x == 45) x = z + y;
Assume x = 4, and y = 6. Which of the following if statements will result in the message "Condition is true" being printed on the screen? if (y <= x) cout << "Condition is true." if (y <= x + 2) cout << "Condition is true." if (y != x + 2) cout << "Condition is true." if (x == y - 2) cout << "Condition is true." if (x < y) cout << "Condition is true."
if (y <= x + 2) cout << "Condition is true." if (x == y - 2) cout << "Condition is true." if (x < y) cout << "Condition is true."
Assume that name is a string variable whose value is "Amanda". Which of the following relational expressions will evaluate to true? (Choose all that apply.) name > "Stacey" name != "Andy" name == "Amanda" name < "Nancy" name < "Alana" name > "Abby"
name > "Abby" name == "Amanda" name != "Andy" name < "Nancy"
Which of the following statements multiplies pay_rate by 1.5 if hours_worked is greater than 40? if (hours_worked > 40) pay_rate += 1.5; if (hours_worked < 40) pay_rate = pay_rate * 1.5; if (hours_worked > 40) pay_rate = pay_rate * 1.5 if (hours_worked < 40) pay_rate *= 1.5; none of these
none of these
The expression x < y is called a
relational expression & a Boolean expression
The following operators are called what? >, <, >=, <=, ==, !=
relational operators
The ______ of a variable is limited to the block in which it is defined.
scope
For Program 4-17 on p. 187, if the user enters 33,000 for income (line 16) and 3 for years (line 21), which cout statement will be executed in the if/else clause?
the one that starts on line 26
If x = 10 and y = 20, which of the following conditions evaluate to true? (Choose all that apply.) x > 50 && y < 30 x < y && y == 20 x == y || y == 6 x > 50 || y < 30 x == 10 && y == 10 x > 0 && y > 5 x < 0 || y < 10 x == 10 || y == 20
x < y && y == 20 x > 50 || y < 30 x > 0 && y > 5 x == 10 || y == 20
If x = 15 and y = 10, what will be the values of x and y after the following code segment executes? if (x > 10) { x = x - 3; y = 2 * y; }
x = 12 and y = 20
If x = 30 and y = 20, what will be the values of x and y after the following code segment executes? if (x > 30) x = 2 * x; y = 2 * y - x;
x = 30 and y = 10
If x = 20 and y = 25, what will be the values of x and y after the following code segment executes? if (y <= 25) { x = x + y; y = y - 5; } x = x - 5;
x = 40 and y = 20
If x = 5 and y = 4, what will be the values of x and y after the following code segment executes? if (x == 6) { x = x + y; y = y - 2; }
x = 5 and y = 4
Which of the following statements about relational expressions is true? x != y is the same as y >= x x <= y is the same as y > x x >= y is the same as y <= x x >= y is the same as y < x
x >= y is the same as y <= x
What is the equivalent English meaning of the condition of the following if statement? if (x > 0 && y < 3) z = 2;
x is greater than 0 AND y is less than 3
What is the equivalent English meaning of the condition of the following if statement? if (x > 0 || y < 3) z = 2;
x is greater than 0 OR y is less than 3
What is the equivalent English meaning of the condition of the following if statement? if (!(x > 4)) z = 2;
x is not greater than 4
Assume that x is 8, y is 5, and z is 2. Which of the following relational expressions are true? x <= 10 z >= (x - y - 1) (x - z) != 6 z != 5 x >= 11 x <= (y * 3) (y * z) == (x + z) (x + y) < (x - z) z < (x + y) y != 5
z < (x + y) z != 5 z >= (x - y - 1) (y * z) == (x + z) x <= (y * 3) x <= 10
The code segment below can be rewritten as what conditional expression? if (x > y) z = 5; else z = 10;
z = x > y ? 5 : 10;