Expressions, Types and Memory · Short answer · 2 marks
Variable age refers to the int 18. The statement print('Age: ' + age) raises an error.
In one sentence, explain why. [1]
Rewrite the statement so that it prints Age: 18 and still uses the + operator. [1]
(a) + cannot combine a str with an int (TypeError); Python does not convert age to a string automatically.
(b) print('Age: ' + str(age))
Functions and the Design Recipe · Short answer · 2 marks
Consider the function below.
def mystery(n: int) -> int:
"""Precondition: 10 <= n <= 99
"""
return n % 10 * 10 + n // 10What does mystery(52) return? [1]
Give an argument for which mystery returns 9. [1]
(a) 25 (b) 90, since 0 * 10 + 9 is 9. (9 itself violates the precondition.)
Strings · Predict the output · 3 marks
Write the output of the code fragment. If the code raises an error, write ERROR and stop.
letters = 'abcdefghij'
print(letters[::2])
print(letters[1::3])
print(letters[::-1])
print(letters[8:2:-2])
print(letters[2:8:-1] == '')
print(letters[-2::-4])acegi
beh
jihgfedcba
ige
True
ieaWith a negative step the start must be to the right of the stop; otherwise the slice is empty.
For and While Loops · Predict the output · 2 marks
Write the output of each code fragment. If the code raises an error, write ERROR and stop.
a = 'abc'
b = '12345'
i = 0
result = ''
while i < len(a) or i < len(b):
if i < len(a):
result = result + a[i]
if i < len(b):
result = result + b[i]
i = i + 1
print(result, i)[1]
The same code, with or in the loop condition replaced by and. [1]
(a)
a1b2c345 5(b)
a1b2c3 3With or the loop continues until both strings are used up; with and it stops as soon as the shorter one is.
Reading and Writing Files · Write code · 3 marks
Complete the function body according to its docstring.
def split_by_grade(grade_name: str, pass_name: str,
fail_name: str) -> None:
"""Copy each line of the file called grade_name to the file
called pass_name if its grade is at least 50, and to the file
called fail_name otherwise, keeping the lines in their original
order. Any existing contents of pass_name and fail_name are
replaced.
Precondition: each line of grade_name has the form
student_number,grade and ends with a newline.
""" grade_file = open(grade_name)
pass_file = open(pass_name, 'w')
fail_file = open(fail_name, 'w')
for line in grade_file:
grade = int(line.strip().split(',')[1])
if grade >= 50:
pass_file.write(line)
else:
fail_file.write(line)
grade_file.close()
pass_file.close()
fail_file.close()Both output files must be opened once, before the loop; opening them inside the loop with 'w' keeps only the last line written to each.
Principles and Practices of Computing · University of California, Los Angeles (UCLA, UC Los Angeles) · also written COMSCI30
Make a free, printable COM SCI 30 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 700 original questions and skips the ones you have already seen.
Start with the past exams and tests your instructor or the UCLA library shares: they show what your course actually asks. Most courses only have a few, so once you have worked through them, make fresh COM SCI 30 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 University of California, Los Angeles. Course codes only say which topics a paper covers; papers are original practice material, not past or upcoming exams.