Expressions, Types and Memory · Short answer · 2 marks
Give a value for n between 100 and 999 for which all three expressions below evaluate to True.
n // 10 % 10 == 3
n % 10 == 7
n // 100 == n % 10 - 5237. The tens digit is 3 and the ones digit is 7, and n // 100 (the hundreds digit) must equal 7 - 5, which is 2.
Booleans and Conditionals · Write code · 3 marks
Complete the function body according to its docstring.
def season(month: int, day: int) -> str:
"""Return the season ('winter', 'spring', 'summer' or
'autumn') in the northern hemisphere on the date month/day.
Spring starts on 3/20, summer on 6/21, autumn on 9/22 and
winter on 12/21. Each season includes its start date.
Precondition: month/day is a valid date.
>>> season(3, 19)
'winter'
>>> season(7, 1)
'summer'
>>> season(12, 21)
'winter'
""" if month < 3 or (month == 3 and day < 20):
return 'winter'
elif month < 6 or (month == 6 and day < 21):
return 'spring'
elif month < 9 or (month == 9 and day < 22):
return 'summer'
elif month < 12 or (month == 12 and day < 21):
return 'autumn'
else:
return 'winter'Because the branches are checked in order, each condition only needs the upper end of its season. Late December is winter again, so the final else is needed.
Functions and the Design Recipe · Write code · 1 mark
Complete the function body according to its docstring.
def body_mass_index(weight: float, height: float) -> float:
"""Return the body mass index (weight divided by the square of
height) for a person who weighs weight kg and is height metres
tall, rounded to 1 decimal place.
Precondition: height > 0
>>> body_mass_index(70.0, 1.75)
22.9
>>> body_mass_index(50.0, 1.6)
19.5
""" return round(weight / height ** 2, 1)weight / height * height is wrong: it divides and then multiplies by height again.
Lists, Mutability and Aliasing · Predict the output · 2 marks
Write the output of the code fragment below.
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
print(days[1:4])
print(days[::3])
print(days[5:2:-1])
print(days[-2:])
print(days[4:100], days[5:2])['Tue', 'Wed', 'Thu']
['Mon', 'Thu', 'Sun']
['Sat', 'Fri', 'Thu']
['Sat', 'Sun']
['Fri', 'Sat', 'Sun'] []Slices never raise an error: an end past the list stops at the end, and an empty range gives [].
Tuples and Dictionaries · Find the bug · 2 marks
The function below has a bug.
def total_points(points: dict[str, int]) -> int:
"""Return the sum of the values in points, which maps each team
to the number of points it has.
>>> total_points({'Lions': 3, 'Owls': 1})
4
"""
total = 0
for team in points:
total = total + team
return totalWhat happens when the docstring example is run? Explain why. [1]
Rewrite line 10 so the function is correct. [1]
A TypeError on line 10: looping over a dictionary gives its keys, so team is the string 'Lions', and 0 + 'Lions' is not allowed.
total = total + points[team]
Elements of Computers and Programming · The University of Texas at Austin (UT Austin, UT, UTexas) · also written CS303E
Make a free, printable C S 303E practice exam: choose the topics, the length and the difficulty above, and get an exam-style PDF with a full answer key. Each paper draws on 900 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UT Austin library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh C S 303E practice exams here on the same topics, with full answer keys, as many as you need. The questions here are original, not copies of past exams.
questionbank.dev is independent and not affiliated with The University of Texas at Austin. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.