Supplementary Final Exam Practice Questions

Please note that these final exam practice questions are intended to supplement the Quiz 00-04 practice questions, your previous quizzes, LS/CQ/EX assignments, and lecture content as study materials. The final exam covers content from every unit/quiz, not solely the concepts covered in these questions.

Multiple Choice with Code Snippets

  1. What does the following expression evaluate to?

    13 % 4
    1. 0

    2. 1

    3. 3

    4. 4

    5. 9

  2. What is the output for the following code?

    x: int = 4
    y: int = x
    x += 1
    print(y)
    1. y

    2. 5

    3. 4

    4. x

    5. 1

  3. Consider the following code snippet:

    leaf: list[int] = [3]
    flower: list[int] = leaf
    flower.append(2)

    What would print(flower) output after the code snippet was executed?

    1. [3]

    2. [3, 2]

    3. [5]

    4. [2]

    5. Error

  4. Consider the following code snippet (same as question 3):

    leaf: list[int] = [3]
    flower: list[int] = leaf
    flower.append(2)

    What would print(leaf) output after the code snippet was executed?

    1. [3]

    2. [3, 2]

    3. [5]

    4. [2]

    5. Error

  5. What is the printed output of the following code snippet? If there is an Error, select Error.

    a: str = "swamp"
    b: int = 110
    print(a + b)
    1. swamp110

    2. swamp + 110

    3. swamp

    4. Error

  6. What does the following expression evluate to? What is its data type?

    "293874"[int("73198"[2])]
    1. 1, int

    2. 1, str

    3. 2, int

    4. 2, str

    5. 3, int

    6. 3, str

    7. 9, int

    8. 9, str

    9. Error

SOLUTIONS

    1. 1. Remember, the % (modulo) operator finds the remainder!
    1. 4. y was initialized to be x’s literal value at the time the second line was exected. Since integers are primitive types, x and y are not “linked” or referring to the same location in memory.
    1. [3, 2].
    1. [3, 2]. Because lists are reference types, the line, flower: list[int] = leaf, caused flower to refer to the same exact list[int] stored in memory as leaf. If we did a memory diagram of this code listing, they would refer to the same ID in the heap! When a value was appended to flower, it inherently was appended to leaf’s list, too (because they refer to the same list).
    1. Error. This code snippet would lead to a TypeError; a string and an integer cannot be concatenated together.
    1. 9, str. We would start with the inner-most parentheses for this question: "73198"[2]. Index 2 of the string has the character "1". So, we can plug "1" in in place of that portion of the expression, giving us: "293874"[int("1")]. Now, the next step is to cast the string "1" to an int. After doing so, we have: "293874"[1]. Index 1 of the string has the character "9" (a str).

 

Looping Questions

Use the following list and dictionary in the questions below.

acro: list[str] = ["n", "a", "s", "a"]
missions: dict[str, int] = {"Apollo": 11, "Voyager": 1, "Artemis": 2}
  1. What will be printed? If it will result in an Error, write “Error.”

    for x in range(0, len(acro)):
        print(x)
  2. What will be printed? If it will result in an Error, write “Error.”

    for x in range(0, len(acro)):
        print(acro[x])
  3. What will be printed? If it will result in an Error, write “Error.”

    for x in range(1, len(missions)):
        print(x)
  4. What will be printed? If it will result in an Error, write “Error.”

    for x in range(0, len(missions)):
        print(missions[x])
  5. What will be printed? If it will result in an Error, write “Error.”

    for a in acro:
        print(a)
  6. What will be printed? If it will result in an Error, write “Error.”

    for a in acro:
        print(acro[a])
  7. What will be printed? If it will result in an Error, write “Error.”

    for m in missions:
        print(m)
  8. What will be printed? If it will result in an Error, write “Error.”

    for m in missions:
        print(missions[m])
  9. What will be printed? If it will result in an Error, write “Error.”

    if "COMP110" in missions:
        print("Found!")
    else:
        print("Not yet...")
  10. What will be printed? If it will result in an Error, write “Error.”

    def go(m: dict[str, int]) -> str:
        for thing in m:
            return thing
        return "Other"
    
    print(go(m=missions))

SOLUTIONS

  1. 0
    1
    2
    3
  2. n
    a
    s
    a
  3. 1
    2
  4. Error

  5. n
    a
    s
    a
  6. Error

  7. Apollo
    Voyager
    Artemis
  8. 11
    1
    2
  9. Not yet...
  10. In a function body, as soon as we reach a return statement and evaluate the expression to the right of return, we are exiting the function and returning the return value back to the line where the function was called (the return address)! This is why the repeat block of this for loop was only executed once.

    Apollo

 

Recursion Memory Diagram

  1. Complete a memory diagram for the following code:

    1   def woo(n: int) -> int:
    2       if n == 1 or n == 2:
    3           return 1
    4       else:
    5           return n + woo(n - 1) * woo(n - 2)
    6
    7 
    8    print(woo(4))

Please view the final exam review session recording for a walkthrough of the solution.

OOP Class-Writing and Memory Diagram

  1. Consider the following class:

    class Athlete:
        name: str
        number: int
    
        def __init__(self, name: str, num: int):
            self.name = name
            self.number = num
    
        def __str__(self) -> str:
            return f"Athlete: {self.name} (#{self.number})"

    Write a class named Team with the following specifications:

    • The class has two attributes:
      • team_name (a str)
      • roster (a dict, where the keys are ints, and the values are Athlete objects)
    • The constructor takes in self and name (a str). The self object’s team_name attribute will be initialized to the name parameter’s literal value, and the roster attribute will be initialized to an empty dictionary.
    • The class has a method named add that takes in self, name (a str), and desired_num (an int), and returns an Athlete object. The purpose of the method will be to add an Athlete object to a Team object’s roster. If the athlete’s desired number is not already taken, they will be assigned that desired number! If it’s already taken, the athlete will be assigned the number 0. To code this behavior, your method should do the following:
      • The method will check if desired_num is already a key in the self object’s roster dictionary.
        • If it is NOT already a key, declare and initialize a new Athlete variable with the athlete’s name and desired_num as its attribute values. Add that desired_num and new Athlete object as a new key-value pair in self’s roster.
        • If it IS already a key (representing someone on the team already having that number), declare and initialize a new Athlete variable with the athlete’s name and 0 as its attribute values. Add that 0 and the new Athlete object as a new key-value pair in self’s roster.
      • The method should return the new Athlete object you created.

(See one solution below)

  1. Complete a memory diagram for the code listing that includes the Athlete class and the Team class you just wrote:

    1   class Athlete:
    2       name: str
    3       number: int
    4 
    5       def __init__(self, name: str, num: int):
    6           self.name = name
    7           self.number = num
    8
    9       def __str__(self) -> str:
    10          return f"Athlete: {self.name} (#{self.number})"
    11 
    12 
    13   class Team:
    14      team_name: str
    15      roster: dict[int, Athlete]
    16 
    17      def __init__(self, name: str):
    18          self.team_name = name
    19          self.roster = dict()
    20
    21      def add(self, name: str, desired_num: int) -> Athlete:
    22          new_athlete: Athlete
    23          if desired_num not in self.roster:
    24              new_athlete = Athlete(name, desired_num)
    25              self.roster[desired_num] = new_athlete
    26          else:
    27              new_athlete = Athlete(name, 0)
    28              self.roster[0] = new_athlete
    29          return new_athlete
    30
    31
    32  unc: Team = Team(name="UNC Women's BB")
    33  kate = unc.add("Kate", 15)
    24  print(kate)

Please view the final exam review session recording for a walkthrough of the solution.

Contributor(s): Izzi Hinks, Alyssa Lytle