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
.
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
.
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
.
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.
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?
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.
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.
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'))
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'))
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'
.
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 (float
s)
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 if
s and elif
s!
Question ExplanationFor an if
statement with elif
s, 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.