Chapter 3 Selections

advertisement
Chapter 3 Selections
1.
<, <=, ==, !=, >, >=
2.
true
false
false
true
true
true
3.
The output is
1
1
4.
if (y > 0)
x = 1;
5.
if (score > 90)
pay *= 1.03;
6.
It should be
if (radius >= 0)
{
area = radius * radius * PI;
cout << "The area for the circle of " <<
" radius " << radius << " is " << area;
}
7.
if (score > 90)
pay *= 1.03;
else
pay *= 1.01;
8.
If number is 30,
(a) displays
30 is even
30 is odd
(b) displays
30 is even
If number is 35,
(a) displays
35 is odd
(b) displays
35 is odd
9.
Note: else matches the first if clause. No output if x = 3 and y = 2. Output is “z is
7” if if x = 3 and y = 4. Output is “x is 2” if if x = 2 and y = 2.
true
false
false
x>2
cout << "x is " << x << endl;
y>2
true
int z = x + y;
cout << "z is " << z << endl;
10.
No output if x = 2 and y = 3. Output is “x is 3” if x = 3 and y = 2.
Output is “z is 6” if x = 3 and y = 3.
11.
Consider score is 90, what will be the grade?
12.
a: none
b: Tax is 0.15
c: Amount is 5
Tax is 0.15
d: Amount is not zero (Reason: amount = 0 is to assign 0 to amount. A number 0 is a
boolean false.)
13. a, c, and d are the same. (B) and (C) are correctly indented.
14.
newLine = (count % 10 == 0);
15.
Both are correct. (b) is better.
16. for (a) if number is 14, the output is
14 is even
if number is 15, the output is
15 is multiple of 5
if number is 30, the output is
30 is even
30 is multiple of 5
for (b) if number is 14, the output is
14 is even
if number is 15, the output is
15 is multiple of 5
if number is 30, the output is
30 is even
17.
Yes
18.
5, 34, 1
19.
rand() % 20
10 + rand() % 10
10 + rand() % 41
rand() % 2
cout << RAND_MAX << endl;
20.
34 + rand() % (55 + 1 – 34)
rand() % 1000
rand() % 1000
21.
(true) && (3 > 4)
false
!(x > 0) && (x > 0)
false
(x > 0) || (x < 0)
true
(x != 0) || (x == 0)
true
(x >= 0) || (x < 0)
true
(x != 1) == !(x == 1)
true
22.
(a) (x > 1) && (x < 100) (b) ((x > 1) && (x < 100)) || (x <
0)
23.
(a) -4.5 < x – 5 && x – 5 < 4.5 (b) x – 5 > 4.5 or x – 5 < -4.5
24.
(b) is correct
25.
Yes.
26.
If x is 45, the expression is false;
If x is 67, the expression is true;
If x is 101, the expression is false;
27.
(x < y && y < z) is 1
(x < y || y < z) is 1
!(x < y) is 0
(x + y < z) is 1
(x + y < z) is 0
28. age > 13 && age < 18
29.
weight > 50 || height > 60.
30.
weight > 50 && height > 60.
31.
(weight > 50 || height > 60) && !(weight > 50 && height > 60).
32.
Switch variables must be of char, byte, short, or int data types. If a break
statement is not used, the next case statement is performed. You can always convert a
switch statement to an equivalent if statement, but not an if statement to a switch
statement. The use of the switch statement can improve readability of the program in some
cases. The compiled code for the switch statement is also more efficient than its
corresponding if statement.
33.
y is 2.
if (x + 3 == 6)
y = 1;
y += 1;
34.
x is 17
switch (a) {
case 1: x += 5; break;
case 2: x += 10; break;
case 3: x += 16; break;
case 4: x += 34;
}
a is 1
x += 5;
break
x += 10;
break
x += 16;
break
x += 34;
break
a is 2
a is 3
a is 4
35. Sorted
36.
(A) ticketPrice = (ages >= 16) ? 20 : 10;
(B) cout << ((count % 10 == 0) ? count + "\n" : count + " "); (Note: we use "\n" to replace
endl in the original code.)
37.
(A)
if (x > 10)
score = 3 * scale;
else
score = 4 * scale;
(B)
if (income > 10000)
tax = income * 0.2;
else
tax = income * 0.17
+ 1000;
(C)
if (number
% 3 == 0)
cout << i << endl;
else
cout << j << endl;
38.
The precedence order for boolean operators is ^, &&, and ||
true || true && false
is true
true && true || false
is true
39. True
40. both are false
41. Yes. Yes. Yes.
Download