Quiz 02 Practice


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

  1. Global variables are limited to the scope of the function they are declared in. (T/F)
  2. Variables can have the same name but store different values if they are defined in a different scope. (T/F)
  3. Named constants should be used to store values that may change throughout the program. (T/F)
  4. When using a for...in loop, on the first line of the loop you must specify the type of the variable (variable refers to i in for i in nums). (T/F)

Solutions

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

Solution

Function Writing

  1. Odd and Even: instructions
  2. Short Words: instructions

Solution

Unit Tests Conceptual Questions

  1. All pytest test function names must begin with test_. (T/F)
  2. If a unit test does NOT pass, this means that one of the assertions in the test is False. (T/F)
  3. Test functions should be written in a file with a name followed by _test.py. (T/F)
  4. Pytest is the only unit testing framework available for Python. (T/F)
  5. Unit tests in Pytest are written in separate files from the code they are testing. (T/F)

Solutions

Unit Test Writing

  1. This function finds the first word in a list of str 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. """
  1. This function removes the first word in a list of str 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. """

Solutions

Memory Diagrams

For more practice, make sure to check out the practice memory diagrams.

Solutions

Functions Conceptual Questions Solutions

  1. False
  2. True
  3. False
  4. 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

  1. True
  2. True
  3. True
  4. False. It is the one we use for this class, but there are other frameworks!
  5. 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"]
Contributor(s): Megan Zhang, David Karash, Coralee Vickers, Alyssa Byrnes, Carolyn Pierce