Expressions, Types and Memory · Trace the code · 2 marks
A transit card is used for several rides. Complete the table to show the value of each variable after each line runs.
fare = 3.25
rides = 4
spent = fare * rides
rides = rides + 1
spent = spent + fare| After line | fare | rides | spent |
| 3 | |||
| 4 | |||
| 5 |
Answer: 3.25; 4; 13.0; 3.25; 5; 13.0; 3.25; 5; 16.25
See the completed table above. 3.25 * 4 is the float 13.0, not the int 13. Changing rides on line 4 does not recompute spent.
Strings · Predict the output · 2 marks
Write the output of the code fragment. If the code raises an error, write ERROR and stop.
filename = 'report_final_v2.pdf'
print(filename.startswith('report'), filename.endswith('PDF'))
print(filename.count('_'), filename.count('_', 7))
print(filename.endswith(''), filename[filename.find('.'):])True False
2 1
True .pdfFor and While Loops · Complete the code · 2 marks
Fill in the blanks to complete the function. Do not change any other code.
def every_third(s: str) -> str:
"""Return a string made of the characters of s at indices
1, 4, 7, 10, ...
>>> every_third('abcdefgh')
'beh'
>>> every_third('a')
''
"""
result = ''
for i in range(__________, __________, __________):
result = result + __________
return result for i in range(1, len(s), 3):
result = result + s[i]Lists, Mutability and Aliasing · Write code · 2 marks
Complete the function body according to its docstring.
def remove_all(values: list[str], target: str) -> int:
"""Remove every occurrence of target from values and return the
number of items removed.
>>> shelf = ['atlas', 'novel', 'atlas', 'poems']
>>> remove_all(shelf, 'atlas')
2
>>> shelf
['novel', 'poems']
""" count = 0
while target in values:
values.remove(target)
count = count + 1
return countAlso correct: count = values.count(target) followed by a loop that calls remove count times.
Classes and Objects · Predict the output · 2 marks
Write the output of the code below. If the code raises an error, write ERROR and stop.
class Song:
"""A song with a title, an artist and a length in seconds."""
def __init__(self, title: str, artist: str, seconds: int) -> None:
self.title = title
self.artist = artist
self.seconds = seconds
def __str__(self) -> str:
mins = str(self.seconds // 60)
secs = str(self.seconds % 60)
return self.title + ' by ' + self.artist + ' ' + mins + ':' + secs
track = Song('Rain', 'Ola', 185)
print(track)
print(track.title.upper())
other = track
other.seconds = 245
print(track)Rain by Ola 3:5
RAIN
Rain by Ola 4:5other and track refer to the same object. Note 5 is not padded to 05.
Computer Science Fundamentals I · Western University (Western, Western U, UWO) · also written COMPSCI1026, COMPSCI 1026
Make a free, printable COMPSCI 1026A/B 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 800 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the Western library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COMPSCI 1026A/B 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 Western University. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.