Questions
The quiz itself will be similar in difficulty to this practice worksheet.
Solutions for each problem can be found at the bottom of this page.
Conceptual Questions
- Global variables are limited to the scope of the function they are declared in. (T/F)
- Variables can have the same name but store different values if they are defined in a different scope. (T/F)
- Named constants should be used to store values that may change throughout the program. (T/F)
- All pytest test function names must begin with
test
. - A test will pass if it does not hit a
False
assertion or some other type of error. - Test functions should be written in a file with a name matching the file that the functions are defined in, followed by
_test
. - When using a
for...in
loop, on the first line of the loop you must specify the type of the variable (variable refers toi
infor i in nums
). (T/F) - In Python dictionaries, each dictionary’s value type must match its key type. (T/F)
- Writing a
for...in
loop on adict
loops through the keys of a dictionary. (T/F) - The values in a dictionary cannot be changed once they are assigned. (T/F)
- Explain the similarities and differences between Python’s
list
anddict
.
Memory Diagrams
Produce a memory diagram for the following code snippet, being sure to include its stack and output.
Produce a memory diagram for the following code snippet, being sure to include its stack and output.
Code Tracing
Given the following code snippet, answer the questions below.
14.1. What is the printed output once the code snippet completes?
14.2. What are the values of the global variablesa
,b
, andc
once the code snippet completes?Given the following code snippet, answer the questions below.
15.1. What is the value oflist_1
once the code snippet completes?
15.2. What is the value ofi
on line 13 during the last call tochange_and_check
?
15.3. How many total frames are created on the stack throughout the run of this program (including the globals frame)?
Function Writing
Write a function called
odd_and_even
. Given a list ofints
,odd_and_even
should return a new list containing the elements that are odd and have an even index.
Example Usage:
Write a function named
vowels_and_threes
. Given a string,vowels_and_threes
should return a new string containing the characters that are either at an index that is a multiple of 3 or a vowel (one or the other, not both). You can assume that the input string is all lowercase, for simplicity.
Example Usage:
Write a function called
average_grades
. Given a dictionary withstr
keys andlist[int]
values,average_grades
should return a new dictionary of typedict[str, float]
that maps each student to their average.
Solutions
Conceptual Questions
- False
- True
- False
- True
- True
- True
- False
- False
- True
- False
- Similarities:
Both are collections that can be looped through using afor...in
loop, reference types that live on the heap, mutable, can duplicate values, subscription notation to access values,.pop()
to remove items
Differences:
list
– Index by increasing integers, add items with.append()
, ordered,for...in
loop gives items
dict
– Matched Key-Value pairs, pair with assignment operator=
(dict_name[key] = value
), control over key type (not limited toint
),for...in
gives keys
Note: these are not all of the similarities and differences, just some important ones to remember
Memory Diagrams
Code Tracing
14.1.
[3, 7, 10, 13]
14.2. a = 3, b = 0, c = 13.515.1.
[6, 7, 5, 3, 0]
15.2. 4
15.3. 4
Function Writing
Note: Your solution does not need to be identical to these, these are just two of many possible solutions.