Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.
Chapter 4: Decisions
65 problems · 0 figuresConcepts in Practice 4.1.1 §4.1 source ↗
Consider the following code:
is_fruit = "True" is_vegetable = 0 is_dessert = FalseWhat is the data type of
is_fruit?- a.Boolean
- b.integer
- c.string
Answer
"True" uses quotation marks, so the value is a string.Concepts in Practice 4.1.2 §4.1 source ↗
Consider the following code:
is_fruit = "True" is_vegetable = 0 is_dessert = FalseWhat is the data type of
is_vegetable?- a.Boolean
- b.integer
- c.string
Answer
is_vegetable is an integer and can be used in arithmetic operations.Concepts in Practice 4.1.3 §4.1 source ↗
Consider the following code:
is_fruit = "True" is_vegetable = 0 is_dessert = FalseWhat is the data type of
is_dessert?- a.Boolean
- b.integer
- c.string
Answer
True or False is a Boolean variable.Concepts in Practice 4.1.4 §4.1 source ↗
Consider the following code:
is_fruit = "True" is_vegetable = 0 is_dessert = FalseHow many values can a Boolean variable represent?
- a.2
- b.4
- c.8
Answer
True or False .Concepts in Practice 4.1.5 §4.1 source ↗
Consider the following code:
is_fruit = "True" is_vegetable = 0 is_dessert = FalseWhich is a valid value for a Boolean variable?
- a.
true - b.
True - c.
1
Answer
True True and False are the only two values for a Boolean variable.Concepts in Practice 4.1.6 §4.1 source ↗
Consider the following code:
is_fruit = "True" is_vegetable = 0 is_dessert = FalseSuppose the following is added to the code above:
is_dessert = 0 print(type(is_dessert))What is the output?
- a.
<class 'bool'> - b.
<class 'int'> - c.
Error
Answer
<class 'int'> int results in implicit type conversion. The type conversion can cause issues later in the code if the programmer assumes is_dessert is still a Boolean.Concepts in Practice 4.1.7 §4.1 source ↗
bool(0.000)- a.
True - b.
False
Answer
False False . Non-zero numbers convert to True .Concepts in Practice 4.1.8 §4.1 source ↗
bool(-1)- a.
True - b.
False
Answer
True True .Concepts in Practice 4.1.9 §4.1 source ↗
bool("")- a.
True - b.
False
Answer
False False .Concepts in Practice 4.1.10 §4.1 source ↗
bool("0")- a.
True - b.
False
Answer
True True .Concepts in Practice 4.1.11 §4.1 source ↗
False, what is bool(input())?- a.
True - b.
False
Answer
Truebool("False") is True .Concepts in Practice 4.1.12 §4.1 source ↗
Given is_on = True, what is the value of each expression?
float(is_on)- a.
0.0 - b.
1.0
Answer
1.0 True converted to a float is 1.0. False converted to a float is 0.0.Concepts in Practice 4.1.13 §4.1 source ↗
Given is_on = True, what is the value of each expression?
str(is_on)- a.
"is_on" - b.
"True"
Answer
"True" True converted to a string is "True" . False converted to a string is "False" .Concepts in Practice 4.1.14 §4.1 source ↗
Given is_on = True, what is the value of each expression?
int(is_on)- a.
0 - b.
1
Answer
1 True is converted to 1 , and False is converted to 0 .Concepts in Practice 4.1.15 §4.1 source ↗
For each new variable, what is the value of compare_result?
x = 14 compare_result = (x <= 13)
- a.
True - b.
False
Answer
FalseConcepts in Practice 4.1.16 §4.1 source ↗
For each new variable, what is the value of compare_result?
w = 0 compare_result = (w != 0.4)
- a.
True - b.
False
Answer
TrueConcepts in Practice 4.1.17 §4.1 source ↗
For each new variable, what is the value of compare_result?
v = 4 compare_result = (v < 4.0)
- a.
True - b.
False
Answer
FalseConcepts in Practice 4.1.18 §4.1 source ↗
For each new variable, what is the value of compare_result?
y = 2 compare_result = (y > "ab")
- a.
True - b.
False - c.
Error
Answer
ErrorConcepts in Practice 4.1.19 §4.1 source ↗
For each new variable, what is the value of compare_result?
z = "cilantro" compare_result = (z == "coriander")
- a.
True - b.
False
Answer
False "c" and "c" are equal, so "i" is compared to "o" . "i" is not equal to "o" so False is produced, though cilantro and coriander come from the same plant.Concepts in Practice 4.1.20 §4.1 source ↗
For each new variable, what is the value of compare_result?
a = "dog" compare_result = (a < "cat")
- a.
True - b.
False
Answer
False "d" is greater than, not less than, "c" , so False is produced.Concepts in Practice 4.2.1 §4.2 source ↗
if age < 12:
print("Discount for children available")- a.
age - b.
age < 12 - c.
print("Discount for children available")
Answer
age < 12age < 12 is between "if" and ":" and is an expression that evaluates to True or False .Concepts in Practice 4.2.2 §4.2 source ↗
1 print("Have a great day.")
2 if is_raining:
3 print("Don't forget an umbrella!")
4 print("See you soon.")- a.1, 2, 3
- b.1, 2, 4
- c.1, 2, 3, 4
Answer
Concepts in Practice 4.2.3 §4.2 source ↗
1 print("Have a great day.")
2 if is_raining:
3 print("Don't forget an umbrella!")
4 print("See you soon.")- a.1, 2, 3
- b.1, 2, 4
- c.1, 2, 3, 4
Answer
Concepts in Practice 4.2.4 §4.2 source ↗
num = -10, what is the final value of num? if num < 0: num = 25 if num < 100: num = num + 50
- a.
-10 - b.
40 - c.
75
Answer
75-10 < 0 is true, so num is assigned with 25 . Then 25 < 100, so num is assigned with 25 + 50, which is 75 .Concepts in Practice 4.2.5 §4.2 source ↗
10, what is the final value of positive_num? positive_num = int(input("Enter a positive number:"))
if positive_num < 0:
print("Negative input set to 0")
positive_num = 0- a.
10 - b.
0 - c.
Error
Answer
0positive_num is always assigned with 0 . The positive input value is lost.Concepts in Practice 4.2.6 §4.2 source ↗
else branch is taken for which range of x? if x >= 15: # Do something else: # Do something else
- a.
x >= 15 - b.
x <= 15 - c.
x < 15
Answer
x < 15 if branch is taken when x >= 15, so the else branch is taken when x is not greater than or equal to 15 .Concepts in Practice 4.2.7 §4.2 source ↗
x = 40, what is the final value of y? if x > 30: y = x - 10 else: y = x + 10
- a.
30 - b.
40 - c.
50
Answer
3040 > 30, so the body of the if statement is executed. y = 40 - 10 = 30.Concepts in Practice 4.2.8 §4.2 source ↗
y = 50, which is not a possible final value of y? if x < 50: y = y / 2 else: y = y * 2 y = y + 5
- a.
30 - b.
55 - c.
105
Answer
55if-else statement. Depending on x's value, y will either have a final value of 30 or 105 .Concepts in Practice 4.3.1 §4.3 source ↗
- a.yes
- b.no
Answer
2500 < 3000 is True , and 2.5 > 1.0 is True , so Jaden can enter.Concepts in Practice 4.3.2 §4.3 source ↗
hrs_to_close will Darcy to be able to enter?- a.
hrs_to close > 1.0 - b.no such value
Answer
3000 > 3000 is False . False and True is False . False and False is False . So no value of hrs_to_close will allow Darcy to enter.Concepts in Practice 4.3.3 §4.3 source ↗
is_admin = False and is_online = True, what is the value of is_admin and is_online?- a.
True - b.
False
Answer
FalseFalse and True is False .Concepts in Practice 4.3.4 §4.3 source ↗
x = 8 and y = 21, what is the final value of z? if (x < 10) and (y > 20): z = 5 else: z = 0
- a.
0 - b.
5
Answer
5(8 < 10) and (21 > 20) evaluates to True and True, which is True . So the body of the if is executed, and z is assigned with 5 .Concepts in Practice 4.3.5 §4.3 source ↗
days = 21 and is_damaged is False, is the refund processed? if (days < 30) or is_damaged: # Process refund
- a.yes
- b.no
Answer
(21 < 30) or False evaluates to True or False, which is True .Concepts in Practice 4.3.6 §4.3 source ↗
age is there no discount? if (age < 12) or (age > 65): # Apply student/senior discount
- a.
age >= 12 - b.
age <= 65 - c.
(age >= 12)and(age <= 65)
Answer
(age >= 12) and (age <= 65)Concepts in Practice 4.3.7 §4.3 source ↗
a = 9 and b = 10, does the test pass? if (a%2 == 0 and b%2 == 1) or (a%2 == 1 and b%2 == 0): # Test passed else: # Test failed
- a.yes
- b.no
Answer
(9%2==0 and 10%2 ==1) evaluates to (False and False), which is False . (9%2 == 1 and 10%2 == 0) evaluates to (True and True), which is True . False or True is True . The if statement checks whether the numbers form an even-odd or odd-even pair.Concepts in Practice 4.3.8 §4.3 source ↗
x = 13, what is the value of not(x < 10)?- a.
True - b.
False
Answer
Truenot(13 < 10) evaluates to not(False), which is True .Concepts in Practice 4.3.9 §4.3 source ↗
x = 18, is x in the correct range? if not(x > 15 and x < 20): # x in correct range
- a.yes
- b.no
Answer
not(18 > 15 and 18 < 20) evaluates to not(True and True), then not(True), and finally, False .Concepts in Practice 4.3.10 §4.3 source ↗
is_turn = False and timer = 65, what is the final value of is_turn? if timer > 60: is_turn = not is_turn
- a.
True - b.
False
Answer
True65 > 60 is True , so is_turn is assigned with not(False), which is True .Concepts in Practice 4.4.1 §4.4 source ↗
Which part of each expression is evaluated first?
x ** 2 + 6 / 3- a.
6 / 3 - b.
x ** 2 - c.
2 + 6
Answer
x ** 2Concepts in Practice 4.4.2 §4.4 source ↗
Which part of each expression is evaluated first?
not 3 * 5 > 10- a.
3 * 5 - b.
not 3 - c.
5 > 10
Answer
3 * 5Concepts in Practice 4.4.3 §4.4 source ↗
Which part of each expression is evaluated first?
z == 5 and x / 8 < 100- a.
5 and x - b.
x / 8 - c.
8 < 100
Answer
x / 8 and .Concepts in Practice 4.4.4 §4.4 source ↗
How is each expression evaluated?
10 + 3 * 2 / 4- a.
10 + (3 * (2 / 4)) - b.
10 + ((3 * 2) / 4) - c.
(10 + 3) * (2 / 4)
Answer
10 + ((3 * 2) / 4)Concepts in Practice 4.4.5 §4.4 source ↗
How is each expression evaluated?
2 * 2 ** 2 ** 3- a.
2 * ((2 ** 2) ** 3) - b.
2 * (2 ** (2 ** 3)) - c.
((2*2) ** 2) ** 3
Answer
2 * (2 ** (2 ** 3))Concepts in Practice 4.4.6 §4.4 source ↗
How is each expression evaluated?
100 < x > 150- a.
100 < x and x < 150 - b.
100 < x or x > 150 - c.
100 < x and x > 150
Answer
100 < x and x > 150 and .Concepts in Practice 4.4.7 §4.4 source ↗
- a.Equality has precedence over
and. - b.All operators are evaluated right to left.
- c.Order is random when parentheses aren't used.
Answer
and. and .Concepts in Practice 4.4.8 §4.4 source ↗
x = 8 and y = 9, what is the result of the following? x + 3 * y - 5- a.
30 - b.
44 - c.
94
Answer
30x + (3*y) - 5. 8 + (3*9) - 5 = 30.Concepts in Practice 4.4.9 §4.4 source ↗
x = 8 and y = 9, what is the result of the following? (x+3) * (y-5)- a.
30 - b.
44 - c.
94
Answer
44(8+3) * (9-5) = 11 * 4 = 44.Concepts in Practice 4.5.1 §4.5 source ↗
Body 2 when condition_1 is false and condition_2 is true. if condition_1: # Body 1 __ condition_2: # Body 2
- a.
if - b.
elif - c.
else
Answer
elif elif will evaluate condition_2 if condition_1 is False and execute Body 2 if condition_2 is True .Concepts in Practice 4.5.2 §4.5 source ↗
x = 42 and y = 0, what is the final value of y? if x > 44: y += 2 elif x < 50: y += 5
- a.
2 - b.
5 - c.
7
Answer
5x > 44, 42 > 44, is False , so x < 50 is evaluated. 42 < 50 is True , so y = 0 + 5. Only one of the if and elif branches is taken.Concepts in Practice 4.5.3 §4.5 source ↗
x is less than 0, Body 1 executes, else if x equals 0, Body 2 executes. if _________: # Body 1 elif _________: # Body 2
- a.
x < 0
x == 0 - b.
x == 0
x < 0 - c.
x <= 0
[no condition]
Answer
x < 0 x == 0x < 0, Body 1 executes. Else if x == 0, Body 2 executes.Concepts in Practice 4.5.4 §4.5 source ↗
- a.
if condition_1: # Body 1 elif condition_2: # Body 2
- b.
if condition_1: # Body 1 elif condition_2: # Body 2 - c.
elif condition_1: # Body 1 if condition_2: # Body 2
Answer
if condition_1: # Body 1 elif condition_2: # Body 2
elif must be chained to a preceding statement and at the same indentation level.Concepts in Practice 4.5.5 §4.5 source ↗
attendees = 350, what is the final value of rooms? rooms = 1 if attendees >= 100: rooms += 3 if attendees <= 200: rooms += 7 elif attendees <= 400: rooms += 14
- a.
4 - b.
15 - c.
18
Answer
18 if and if-elif statements are not chained. The first if evaluates to True and executes. Then the if-elif is evaluated and the elif branch executes.Concepts in Practice 4.5.6 §4.5 source ↗
hour = 12, what is printed? if hour < 8:
print("Too early")
elif hour < 12:
print("Good morning")
elif hour < 13:
print("Lunchtime")
elif hour < 17:
print("Good afternoon")
else:
print("Too late")- a.
Good morning - b.
Lunchtime - c.
Good afternoon - d.
Too late
Answer
Lunchtimehour < 8 and hour < 12 is False and hour < 13 is True .Concepts in Practice 4.5.7 §4.5 source ↗
elif statement be added?_1_ if condition: # Body _2_ elif condition: # Body _3_ else: # Body _4_
- a.1
- b.2
- c.3
- d.4
Answer
elif can be chained to another elif to form a longer chained decision.Concepts in Practice 4.5.8 §4.5 source ↗
x = -1 and y = -2, what is the final value of y? if x < 0 and y < 0: y = 10 elif x < 0 and y > 0: y = 20 else: y = 30
- a.
10 - b.
20 - c.
30
Answer
10-1 < 0 and -2 < 0 is True , so y = 10.Concepts in Practice 4.5.9 §4.5 source ↗
if price < 9.99: order = 50 if 9.99 <= price < 19.99: order = 30 if price >= 19.99: order = 10
- a.
if price < 9.99: order = 50 else: order = 30 order = 10
- b.
if price < 9.99: order = 50 elif price < 19.99: order = 30 elif price == 19.99: order = 10
- c.
if price < 9.99: order = 50 elif price < 19.99: order = 30 else: order = 10
Answer
if price < 9.99: order = 50 elif price < 19.99: order = 30 else: order = 10
price < 9.99. The second branch executes if price < 19.99 and the first condition fails: that is, price >= 9.99. The third branch executes if the first and second conditions fail: that is, price >= 19.99. Chaining can simplify decision statements.Concepts in Practice 4.6.1 §4.6 source ↗
leaf_count = 9 and leaf_shape = "teardrop", what is the output?- a.
Might be poison ivy - b.
Might be poison oak - c.
Might be poison sumac
Answer
Might be poison sumacleaf_count == 3 is False , so the outer else ' s body is executed.Concepts in Practice 4.6.2 §4.6 source ↗
num_dancers = 49, what is printed? if num_dancers < 0:
print("Error: num_dancers is negative")
else:
if num_dancers % 2 == 1:
print("Error: num_dancers is odd")
print(num_dancers, "dancers")- a.
Error: num_dancers is odd - b.
49 dancers - c.
Error: num_dancers is odd49 dancers
Answer
Error: num_dancers is odd49 dancersnum_dancers is positive, so the else branch is taken. num_dancers is odd, so the error is printed before the program continues after the nested if and prints num_dancers.Concepts in Practice 4.6.3 §4.6 source ↗
x = 256, y = 513, and max = 512, which of the following will execute? if x == y:
# Body 1
elif x < y:
# Body 2
if y >= max:
# Body 3
else:
# Body 4
else:
# Body 5- a.
Body 2 - b.
Body 2,Body 3 - c.
Body 2,Body 5
Answer
Body 2, Body 3256 == 513 is False , and 256 < 513 is True , so the elif branch is taken and Body 2 executes. 513 >= 512 is True , so Body 3 executes.Concepts in Practice 4.6.4 §4.6 source ↗
x =118, y = 300, and max = 512, which of the following will execute? if x == y:
# Body 1
elif x < y:
# Body 2
if y >= max:
# Body 3
else:
# Body 4
else:
# Body 5- a.
Body 2 - b.
Body 3 - c.
Error
Answer
Error else isn't indented and is treated as the else to the original if . Thus the second else is not connected to an if statement and produces an error.Concepts in Practice 4.7.1 §4.7 source ↗
if-else statement? if x%2 == 0: response = 'even' else: response = 'odd'
- a.
response = if x%2 == 0 "even" else "odd" - b.
response = "odd" if x%2 == 0 else "even" - c.
response = "even" if x%2 == 0 else "odd"
Answer
response = "even" if x%2 == 0 else "odd"response with "even" if x is even. response is assigned with "odd" if x is odd.Concepts in Practice 4.7.2 §4.7 source ↗
x = 100 and offset = 10, what is the value of result? result = x + offset if x < 100 else x - offset
- a.
90 - b.
100 - c.
110
Answer
90100 < 100 is False so result is assigned with x - offset = 100 - 10 = 90.Concepts in Practice 4.7.3 §4.7 source ↗
min_num = x if x < y else min_num = y
- a.
min_num = x - b.
x < y - c.
min_num = y
Answer
min_num = ymin_num cannot be assigned with min_num = y. The correct expression is min_num = x if x < y else y"Concepts in Practice 4.7.4 §4.7 source ↗
if-else statement? if x < 50: result = True else: result = False
- a.
result = True if x < 50 else False - b.
result = x < 50
Answer
result = x < 50Concepts in Practice 4.7.5 §4.7 source ↗
total? total = fee + 10 if hours > 12 else 2
- a.
10,2 - b.
fee + 10,2 - c.
fee + 10,fee + 2
Answer
fee + 10, 2(fee + 10) if hours > 12 else 2. So if hours > 12, the result will be fee + 10; otherwise, the result will be 2.Loading chapter 5…
Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.