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)
- 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, heap, and output.
def main() -> None:
"""Entry point of program."""
strings: list[str] = ["hey", "o", "eh", "e"]
word: str = make_word(strings)
print(word)
def make_word(root: list[str]) -> str:
"""A nonsensical function that makes a 'word'."""
word: str = ""
i: int = 0
while i < len(root):
if i % 2 == 0:
word += root[i]
else:
if len(word) < 5:
word += root[i]
else:
return word
i += 1
return word
if __name__ == "__main__":
main()
- Produce a memory diagram for the following code snippet, being sure to include its stack, heap, and output.
"""References Practice."""
def create() -> list[int]:
"""An obnoxious way to make a list."""
list_1: list[int] = []
i: int = 0
while i < 3:
list_1.append(i)
i += 1
return list_1
def increase (a_list: list[int], x: int) -> None:
"""Lets pump it up!"""
i: int = 0
while i < len(a_list):
a_list[i] += x
i += 1
return None
def main() -> None:
"""Entrypoint of the program."""
list_1: list[int] = create()
list_2: list[int] = list_1
list_1 = create()
increase(list_1, 2)
print(list_1)
print(list_2)
if __name__ == "__main__":
main()
- Given the following code snippet, answer the questions below.
def change_and_check(x: int, nums: list[int]) -> int:
"""Let's see what happens!"""
if x < 0:
return 0
i: int = 0
while i < len(nums):
nums[i] += x
i += 1
i = 0
while i < len(nums):
if nums[i] == x:
return 0
i += 1
return x - 1
def main() -> None:
"""The entrypoint of this program."""
num_1: int = 0
list_1: list[int] = [1, 2, num_1]
list_1.append(change_and_check(2, list_1))
list_1.append(change_and_check(3, list_1))
main()
4.1. What is the value of list_1
once the code snippet completes?
4.2. What is the value of i
on line 10 during the last call to change_and_check
?
4.3. How many total frames are created on the stack throughout the run of this program (including the globals frame)?
Function Writing
- Odd and Even: instructions
- Value Exists: instructions
- Short Words: instructions
Autograder instructions:
Join the Gradescope class for practice problems with the code: 4V7PB5 (Do this by going to your Gradescope homepage and clicking on “Enroll in Course”) Do not compress these files before submitting. Simply submit the .py
files.
More Practice
For more practice, make sure to check out our page of practice memory diagrams. For QZ02, we especially recommend doing the following diagrams:
Solutions
Conceptual Questions
- False
- True
- False
- 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
3.1. [6, 7, 5, 3, 0]
3.2. 4
3.3. 4
Function Writing
Note: Your solution does not need to be identical to these, these are just examples of one of many possible solutions.
def odd_and_even(list1: list[int]) -> list[int]:
"""Find the odd elements with even indexes."""
i: int = 0
list2: list[int] = []
while i < len(list1):
if list1[i] % 2 == 1 and i % 2 == 0:
list2.append(list1[i])
i += 1
return list2
def value_exists(inp_dict: dict[str,int], val: int) -> bool:
"""Return true if val is in inp_dict"""
exists: bool = False
for elem in inp_dict:
if inp_dict[elem] == val:
exists = True
return exists
def short_words(inp_list: list[str]) -> list[str]:
"""Filter out the shorter words"""
ret_list = []
for x in inp_list:
if len(x) < 5:
ret_list.append(x)
else:
print(f"{x} is too long!")
return ret_list