Answer the following questions. Some of the questions will require you
to run code in IDLE's Python shell. (You should do this anyway to get
used to using IDLE.)
Question 1
Which of the following results in a SyntaxError
?
Question ExplanationTry typing each option exactly as it
appears into IDLE, paying particular attention to newlines. Copying and
pasting might not work, so if you're frustrated, try typing them out.
Make sure you understand why each one does what it does!
Question 2
Which of the following results in a SyntaxError
?
Question ExplanationTry typing each option exactly as it
appears into IDLE, paying particular attention to which quote (single,
double or triple) is used. Make sure you understand why each one does
what it does!
Question 3
The following is printed by a
print
function call:
yesterday
today
tomorrow
Select the function call(s) that prints what is shown above.
Question ExplanationTry typing each option exactly as it
appears into IDLE, paying particular attention to newlines. Copying and
pasting might not work, so if you're frustrated, try typing them out.
Make sure you understand why each one does what it does!
Question 4
The following is printed by a
print
function call:
hello-how-are-you
Select the function call(s) that prints what is shown above.
Question ExplanationTry each of the options in IDLE to
see which one produces the correct output. Make sure you understand why
each one does what it does!
Question 5
Consider this code fragment:
>>> def announce_location(country):
# Missing function body
>>> instructor_location = announce_location('Canada')
>>> print(instructor_location)
Canada
Select the missing function body from the options below.
Question ExplanationYou should try each of the options in
IDLE to see which one produces the correct output. Make sure you
understand why each one does what it does!
Question 6
Consider this code fragment:
>>> def announce_location(country):
# Missing function body
>>> instructor_location = announce_location('Canada')
Canada
>>> print(instructor_location)
Canada
Select the missing function body from the options below.
Question ExplanationYou can (and should) try each of the options in IDLE to see what they do. Make sure you understand how each of them works.
If the print
function call comes after the return
statement, it will not be reached since the return
statement causes the function to exit immediately.
Question 7
Consider the following statements:
x = None
print(x)
What is printed when the code above executes?
You entered:
Your Answer |
|
Score |
Explanation |
None |
Correct |
1.00 |
|
Total |
|
1.00 / 1.00 |
|
Question ExplanationType the code into IDLE and copy and
paste what gets displayed in IDLE. Be careful about capitalization, and
make sure you enter the two statements on different lines. The code
should not generate an error.
Question 8
What is the first step of the Design Recipe?
Question ExplanationWhat is the first step you should take when designing a function? The steps of the Design Recipe are discussed in the lectures.
Question 9
What is the last step of the Design Recipe?
Question ExplanationThe steps of the Design Recipe are discussed in the lectures.
Question 10
What is the
Type Contract for the following function definition?
def is_passing_grade(grade):
"""
Return 'pass' if grade is at least 50 and return 'fail' otherwise.
>>> is_passing_grade(45)
'fail'
>>> is_passing_grade(80.5)
'pass'
"""
Question ExplanationWe use number
if the function accepts both int
s and float
s. Also, Python's string type is str
.
When writing your type contract, be as specific as possible. For
example, only use "number" if it makes sense for the function to return
both float
s and int
s (not just float
s that happen to be integers).
Question 11
What is the
Type Contract for the following function definition?
def total_vowels(word1, word2):
"""
Return the number of vowels in words word1 and word2.
>>> total_vowels('hello', 'hi')
3
"""
Question ExplanationWe use number
if the function accepts both int
s and float
s. Also, Python's string type is str
.
When writing your type contract, be as specific as possible. For
example, only use "number" if it makes sense for the function to return
both float
s and int
s (not just float
s that happen to be integers).
Question 12
According to the
Description of function
get_oldest
, what value should be returned by the
Example function call?
def get_oldest(age1, age2):
''' (int, int) -> int
Return the oldest of the two ages, age1 and age2.
>>> get_oldest(27, 22)
???
'''
You entered:
Your Answer |
|
Score |
Explanation |
27 |
Correct |
1.00 |
|
Total |
|
1.00 / 1.00 |
|
Question ExplanationEnter what should be in the docstring in place of "???"
Question 13
Here is an insufficient docstring for function
euro_to_dollars
:
def euro_to_dollars(amount):
"""(number) -> number
Calculate the value in Canadian dollars of the given quantity of Euros.
"""
Identify the problem(s) with the
Description in the docstring above.
Question ExplanationFunction docstrings need to mention
the parameters by name, and if there is a return type the docstring
needs to explain what is returned.
Question 14
Two function definitions are saved in the same file:
- A function
count_vowels
has one parameter, a word, and returns the number of vowels in that word.
- A function
count_consonants
has one parameter, a word, and returns the number of consonants in that word.
To determine the number of letters in a word, write a one-line body for the following function that calls both
count_vowels
and
count_consonants
:
def count_letters(word):
""" (str) -> int
Return the number of letters in word.
>>> count_letters('hello')
5
>>> count_letters('bonjour')
7
"""
# Write the one-line function body that belongs here.
Note:
- do not call any functions other than those listed above
- do not use any unnecessary parentheses
You entered:
Your Answer |
|
Score |
Explanation |
return count_vowels(hello), count_consonants(bonjour) |
Incorrect |
0.00 |
|
Total |
|
0.00 / 1.00 |
|
Question ExplanationYou need to write a return
statement, and you need to call both count_vowels(...)
and count_consonants(...)
. Remember that there needs to be a space after the word "return".
Question 15
Two function definitions are saved in the same file:
- A function
get_capital
has one string parameter that represents a country and returns its capital.
- A function
longer
has two string parameters and returns the longer of the two strings.
Variables
country1
and
country2
refer to
str
values. Write a one-line
expression that produces the longer of the capitals of
country1
and
country2
. Your expression should involve calls on both
get_capital
and
longer
.
Note:
- do not call any functions other than those listed above
- do not use any unnecessary parentheses
You entered:
Your Answer |
|
Score |
Explanation |
longer (get_capital(country1), get_capital(country2)) |
Correct |
1.00 |
|
Total |
|
1.00 / 1.00 |
|
Question ExplanationWrite just an expression, not a return
statement or an assignment statement or a print statement. Make sure all your opening and closing parentheses match. country1
and country2
are variables, and so should not be quoted; that makes them strings. Do not use more parentheses than necessary.
Question 16
What is the value of
average
after the following code is executed?
grade1 = 80
grade2 = 90
average = (grade1 + grade2) / 2
grade1 = 100
Question ExplanationBe sure to use Python 3, and check your answer by running the code in IDLE. You can then call print(average)
to find out it's value.
Question 17
Below is an image of the Python Visualizer in action. The line with
the red arrow (line 15) is about to be executed. When we press Forward, function convert_to_minutes
will be called, control will move to line 11 of the code (the first
line of that function), and a new stack frame will be created containing
variable num_hours
. What value will num_hours
refer to then? (We are looking for a value, not a memory address.)
(If the image is too small, right-click on it and open it in a new browser tab. Then you can zoom in.)
You entered:
Your Answer |
|
Score |
Explanation |
25 |
Correct |
1.00 |
|
Total |
|
1.00 / 1.00 |
|
Question ExplanationA great way to understand this
question is to run it in the Python Visualizer, which is available in
the Resources link in the navigation bar.
The line with the red arrow (line 15) is about to be executed. After
stepping through to the end of the code below (we can do this by
pressing the Last button), how many variables (excluding those
that refer to functions) will be on the stack? Recall that the stack is
represented by the images on the left-hand side of the model. (If the
image is too small, right-click on it and open it in a new browser tab.
Then you can zoom in.)
Question 18
Question ExplanationA great way to understand this
question is to run it in the Python Visualizer, which is available in
the Resources link in the navigation bar.
Variable result
will not remain on the stack at the end of execution. It is a local variable for function convert_to_minutes
, and is no longer accessible when the function has exited.
Remember to exclude variables that refer to functions.