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.
Functions 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)
for Loops
1.1 Rewrite the following code snippet with same functionality using a for ... in
loop. 1.2 Rewrite the following code snippet with same functionality using a for ... in range(...)
loop.
stats: list[int] = [7, 8, 9]
index: int = 0
total: int = 100
while index < len(stats):
total -= stats[index]
index += 1
Function Writing
- Odd and Even: instructions
- Short Words: instructions
Unit Tests Conceptual Questions
- All pytest test function names must begin with
test_
. (T/F) - If a unit test does NOT pass, this means that one of the assertions in the test is False. (T/F)
- Test functions should be written in a file with a name followed by
_test.py
. (T/F) - Pytest is the only unit testing framework available for Python. (T/F)
- Unit tests in Pytest are written in separate files from the code they are testing. (T/F)
Unit Test Writing
- This function finds the first word in a
list
ofstr
with an evenlength.
def find_even(words: list[str]) -> str:
idx: int = 0
while idx < len(words):
if len(words[idx]) % 2 == 0:
return words[idx]
idx += 1
return ""
Fill in this unit test with a use case.
def test_find_even_use_case() -> None:
""" Put code here. """
- This function removes the first word in a
list
ofstr
with an even length.
def remove_first_even(words: list[str]) -> None:
idx: int = 0
condition: bool = True
while (idx < len(words)) and condition:
if len(words[idx]) % 2 == 0:
words.pop(idx)
condition = False
idx += 1
Fill in this unit test with a use case.
def test_remove_first_even_use_case() -> None:
""" Put code here. """
Memory Diagrams
For more practice, make sure to check out the practice memory diagrams.
Solutions
Functions Conceptual Questions Solutions
- False
- True
- False
- False
for Loop Solutions
1.1
stats: list[int] = [7, 8, 9]
total: int = 100
for elem in stats:
total -= elem
1.2
stats: list[int] = [7, 8, 9]
total: int = 100
for index in range(0, len(stats)):
total -= stats[index]
Function Writing Solutions
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 short_words(inp_list: list[str]) -> list[str]:
"""Filter out the shorter words"""
ret_list: list[str] = []
for x in inp_list:
if len(x) < 5:
ret_list.append(x)
else:
print(f"{x} is too long!")
return ret_list
Unit Tests Conceptual Questions Solutions
- True
- True
- True
- False. It is the one we use for this class, but there are other frameworks!
- True
Unit Test Writing Solutions
These can have many possible answers! Here is just one example.
def test_find_even_use_case() -> None:
test_input: list[str] = ["Hello", "Hi", "Howdy", "Hiya"]
assert find_even(test_input) == "Hi"
def test_remove_first_even_use_case() -> None:
test_input: list[str] = ["Hello", "Hi", "Howdy", "Hiya"]
remove_first_even(test_input)
assert test_input == ["Hello", "Howdy", "Hiya"]