Score of 16.00 out of 16.00.
You should run any code that you are unsure of, in IDLE or in the Python Visualizer (or both).

Question 1

Variable dollars refers to the value 8. Select the expression(s) that produce True.
Your Answer
Score Explanation
8 >= dollars Correct 0.25
dollars != 8 Correct 0.25
8 = dollars Correct 0.25
dollars == 8.0 Correct 0.25
Total
1.00 / 1.00
Question ExplanationThe equality operator is ==, and the assignment operator is =. When the equality operator (==) is applied to int value 8 and float value 8.0, the two values are considered equivalent.

Question 2

Variable instructors refers to the value 2. Select the expression(s) that produce True.
Your Answer
Score Explanation
not not instructors >= -3 Correct 0.25
not instructors * 2 > 1 Correct 0.25
instructors < 2 or instructors > 4 Correct 0.25
instructors < 0 or instructors > 1 Correct 0.25
Total
1.00 / 1.00
Question Explanationand evaluates to True if and only if both operands are True.
or evaluates to True if at least one of the operands is True.
not True is False and not False is True.
Boolean operators (like not and or) have lower precedence (happen later) than comparative operators (like >= and ==).

Question 3

Variable dollars refers to the value 18 and variable cents refers to the value 53. Select the expression(s) that produce True.
Your Answer
Score Explanation
not (dollars < cents and dollars > 0) Correct 0.25
not dollars < 10 and cents > 15 Correct 0.25
(not dollars == 18) or cents == 53 Correct 0.25
not dollars == 18 or not cents == 53 Correct 0.25
Total
1.00 / 1.00
Question Explanationand evaluates to True if and only if both operands are True.
or evaluates to True if at least one of the operands is True.
not True is False and not False is True.
not has higher precedence than and and or.
Boolean operators (like not and or) have lower precedence (happen later) than comparative operators (like >= and ==).

Question 4

The following is a valid Python expression, where x is a variable that refers to an int value:
8 > x >= 5
Select the expression that is equivalent to the one above. Remember that you can try this out in the Python shell.
Your Answer
Score Explanation
8 > x or x >= 5


not (8 > x and x >= 5)


x > 8 and x >= 5


8 > x and x >= 5 Correct 1.00
Total
1.00 / 1.00
Question ExplanationIn English, the expression 8 > x >= 5 means: 8 is greater than x and x is greater than or equal to 5.

Question 5

What does the expression int(72.5) produce?
Your Answer
Score Explanation
'72'


72.0


73


72 Correct 1.00
Total
1.00 / 1.00
Question ExplanationPart of the help for int says this:
Convert a string or number to an integer, if possible. 
You can also try this in the Python shell.

Question 6

if eggs % 12 == 0:
    return False
else:
    return True
Select the statement(s) that are equivalent to the code fragment above.
Your Answer
Score Explanation
return eggs % 12 == 0 Correct 0.25
return not eggs % 12 == 0 Correct 0.25
return eggs % 12 != 0 Correct 0.25
return not (eggs % 12 != 0) Correct 0.25
Total
1.00 / 1.00
Question ExplanationTry setting eggs to several different values and seeing what eggs % 12 == 0 evaluates to, and then think about how that relates to what is returned.
Because this code fragment has return statements in it, you can't simply copy and paste it into the Python shell. Try putting this code fragment into a function that has a parameter named eggs.

Question 7

Consider this code:
age1 = input("How old are you? ")
age2 = input("How old is your best friend? ")
The user enters the ages in years as whole numbers (e.g., 2). Select the code fragment(s) that print the sum of the ages. Be sure to use Python 3.
Your Answer
Score Explanation

print(str(int(age1 + age2)))
Correct 0.25

print(int(age1) + int(age2))
Correct 0.25

x = int(age1)
y = int(age2)
print(str(x + y))
Correct 0.25

print(age1 + age2)
Correct 0.25
Total
1.00 / 1.00
Question ExplanationUse help(input) to see what type of value the input function returns. You should also run this code in the Python IDLE shell to try the various options and determine the correct answer(s).

Question 8

The math module has a function that finds the ceiling of a number (the smallest integral value greater or equal to the number). Assuming that the math module has already been imported, write an expression that calls the ceiling function from math to find the ceiling of 84.2.
Hint: In IDLE, import math and then use dir and help on the math module to determine the name of the function that you need to use.

You entered:

Your Answer
Score Explanation
math.ceil(84.2) Correct 1.00
Total
1.00 / 1.00
Question ExplanationTo access a function from math, use math.function_name
math.ceiling is not a function. Run import math and then use dir(math) to figure out the correct name of the function to compute the ceiling of a number.

Question 9

A program in book.py uses functions defined in chapter.py. In order to call functions from the chapter module, what statement must appear in the book module?

You entered:

Your Answer
Score Explanation
import chapter Correct 1.00
Total
1.00 / 1.00
Question ExplanationTo use functions from one module in another, the module must first be imported.

Question 10

What is printed when this code is executed?
def traffic_report(light):
    if light == 'red':
        return 'stop'
    elif light == 'yellow':
        return 'slow'
    elif light == 'green':
        return 'go'

print(traffic_report('yellow'))
Your Answer
Score Explanation
None


go


slow Correct 1.00
stop
slow
go



stop


Total
1.00 / 1.00
Question ExplanationAt most one of the if and elif clauses is executed. Run this code in IDLE to check your answer.

Question 11

What is printed when this code is executed?
def traffic_report(light):
    if light == 'red':
        return 'stop'
    elif light == 'yellow':
        return 'slow'
    elif light == 'green':
        return 'go'

print(traffic_report('orange'))
Your Answer
Score Explanation
stop
slow
go



slow


None Correct 1.00
go


stop


Total
1.00 / 1.00
Question ExplanationNone of the if or elif conditions is True, so none of the return statements is executed. Try running this code in IDLE.

Question 12

Consider the following function definition:
def weather_report(temp):
    if temp >= 20:
        return 'warm enough for ice cream'
    elif temp >= 0:
        return 'above freezing'
Select the function call(s) that produce 'warm enough for ice cream'.
Your Answer
Score Explanation
weather_report(30) Correct 0.25
weather_report(10) Correct 0.25
weather_report(-5) Correct 0.25
weather_report(20) Correct 0.25
Total
1.00 / 1.00
Question ExplanationAt most one of the if and elif clauses is executed. Run this code in IDLE to check your answer.

Question 13

grade1 and grade2 represent two grades (floats) between 0.0 and 100.0, inclusive. A passing grade is greater than or equal to 50. Select the code fragment(s) that prints the average of all passing grade(s). The printed value should be 0.0 if neither grade is a passing grade, the passing grade if exactly one grade is a passing grade, and the average of the two grades if both are passing grades.

Watch your ifs and elifs!
Your Answer
Score Explanation

total = 0
grade_count = 0

if grade1 >= 50:
    total = total + grade1
    grade_count = grade_count + 1
    if grade2 >= 50:
        total = total + grade2
        grade_count = grade_count + 1

if grade_count > 0:
    print(total / grade_count)
else:
    print(0.0)
Correct 0.25 Consider what happens when the first grade is not a passing grade, but the second grade is.

total = 0
grade_count = 0

if grade1 >= 50:
    total = total + grade1
    grade_count = grade_count + 1
if grade2 >= 50:
    total = total + grade2
    grade_count = grade_count + 1

if grade_count > 0:
    print(total / grade_count)
else:
    print(0.0)
Correct 0.25

total = 0
grade_count = 0

if grade1 >= 50:
    total = total + grade1
    grade_count = grade_count + 1
elif grade2 >= 50:
    total = total + grade2
    grade_count = grade_count + 1

if grade_count > 0:
    print(total / grade_count)
else:
    print(0.0)
Correct 0.25 Consider what happens when both grades are passing grades.

total = 0
grade_count = 0

if grade1 >= 50:
    total = total + grade1
    grade_count = grade_count + 1
else:
    total = total + grade2
    grade_count = grade_count + 1

if grade_count > 0:
    print(total / grade_count)
else:
    print(0.0)
Correct 0.25 Consider what happens when both grades are failing grades.
Total
1.00 / 1.00
Question ExplanationFor an if statement with elifs, the body of at most one of those clauses is executed.

If there are multiple if statements, then multiple bodies can be executed.

Run the code in IDLE or the Python Visualizer.

Question 14

Including the frame for the main program, how many stack frames exist at the current point of execution (indicated by the Python Visualizer with blue highlighting)? Remember that the red arrow indicates the line that is about to be executed.
To answer this, you should use the Python Visualizer for this code: greeting.py

(Note: we omitted docstrings in this code to save space. Don't do this in your own code!)

You entered:

Your Answer
Score Explanation
1 Correct 1.00
Total
1.00 / 1.00
Question ExplanationThe first stack frame is for the main part of your program. Every time a function is called, a new stack frame is created, and as soon as a function finishes running, the stack frame for that function is removed.

Question 15

Including the frame for the main program, how many stack frames exist at the current point of execution (indicated by the Python Visualizer with blue highlighting)? Remember that the red arrow indicates the line that is about to be executed.
To answer this, you should use the Python Visualizer for this code: greeting.py

(Note: we omitted docstrings in this code to save space. Don't do this in your own code!)

You entered:

Your Answer
Score Explanation
3 Correct 1.00
Total
1.00 / 1.00
Question ExplanationThe first stack frame is for the main part of your program. Every time a function is called, a new stack frame is created, and as soon as a function finishes running, the stack frame for that function is removed.

Question 16

Including the frame for the main program, how many stack frames exist at the current point of execution (indicated by the Python Visualizer with blue highlighting)? Remember that the red arrow indicates the line that is about to be executed.
To answer this, you should use the Python Visualizer for this code: greeting.py

(Note: we omitted docstrings in this code to save space. Don't do this in your own code!)

You entered:

Your Answer
Score Explanation
3 Correct 1.00
Total
1.00 / 1.00
Question ExplanationThe first stack frame is for the main part of your program. Every time a function is called, a new stack frame is created, and as soon as a function finishes running, the stack frame for that function is removed.
Before entering the exclaim function, its arguments have already been evaluated, meaning greeting has already returned, and thus its stack frame has been erased.