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
What does the following expression evaluate to?
13 % 401349
What is the output for the following code?
x: int = 4 y: int = x x += 1 print(y)y54x1
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?[3][3, 2][5][2]Error
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?[3][3, 2][5][2]Error
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)swamp110swamp + 110swampError
What does the following expression evluate to? What is its data type?
"293874"[int("73198"[2])]1, int1, str2, int2, str3, int3, str9, int9, strError
SOLUTIONS
1. Remember, the % (modulo) operator finds the remainder!
4.ywas initialized to bex’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.
[3, 2].
[3, 2]. Because lists are reference types, the line,flower: list[int] = leaf, causedflowerto refer to the same exactlist[int]stored in memory asleaf. 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 toflower, it inherently was appended toleaf’s list, too (because they refer to the same list).
Error. This code snippet would lead to a TypeError; a string and an integer cannot be concatenated together.
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}What will be printed? If it will result in an Error, write “Error.”
for x in range(0, len(acro)): print(x)What will be printed? If it will result in an Error, write “Error.”
for x in range(0, len(acro)): print(acro[x])What will be printed? If it will result in an Error, write “Error.”
for x in range(1, len(missions)): print(x)What will be printed? If it will result in an Error, write “Error.”
for x in range(0, len(missions)): print(missions[x])What will be printed? If it will result in an Error, write “Error.”
for a in acro: print(a)What will be printed? If it will result in an Error, write “Error.”
for a in acro: print(acro[a])What will be printed? If it will result in an Error, write “Error.”
for m in missions: print(m)What will be printed? If it will result in an Error, write “Error.”
for m in missions: print(missions[m])What will be printed? If it will result in an Error, write “Error.”
if "COMP110" in missions: print("Found!") else: print("Not yet...")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
0 1 2 3n a s a1 2Errorn a s aErrorApollo Voyager Artemis11 1 2Not yet...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
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
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
Teamwith the following specifications:- The class has two attributes:
team_name(astr)roster(adict, where the keys areints, and the values areAthleteobjects)
- The constructor takes in
selfandname(astr). Theselfobject’steam_nameattribute will be initialized to thenameparameter’s literal value, and therosterattribute will be initialized to an empty dictionary. - The class has a method named
addthat takes inself,name(astr), anddesired_num(anint), and returns anAthleteobject. The purpose of the method will be to add anAthleteobject to aTeamobject’sroster. 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 number0. To code this behavior, your method should do the following:- The method will check if
desired_numis already a key in theselfobject’srosterdictionary.- If it is NOT already a key, declare and initialize a new
Athletevariable with the athlete’snameanddesired_numas its attribute values. Add thatdesired_numand newAthleteobject as a new key-value pair inself’sroster. - If it IS already a key (representing someone on the team already having that number), declare and initialize a new
Athletevariable with the athlete’snameand0as its attribute values. Add that0and the newAthleteobject as a new key-value pair inself’sroster.
- If it is NOT already a key, declare and initialize a new
- The method should return the new
Athleteobject you created.
- The method will check if
- The class has two attributes:
(See one solution below)
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.