xuebaunion@vip.163.com
3551 Trousdale Rkwy, University Park, Los Angeles, CA
留学生论文指导和课程辅导
无忧GPA:https://www.essaygpa.com
工作时间:全年无休-早上8点到凌晨3点
微信客服:xiaoxionga100
微信客服:ITCS521
Question/Answer Booklet COMPSCI 101
Page 1 of 14
THE UNIVERSITY OF AUCKLAND
SUMMER SEMESTER, 2020
Campus: City
COMPUTER SCIENCE
Principles of Programming
(Time Allowed: TWO hours)
NOTE:
You must answer all questions in this exam.
No calculators are permitted.
Answer in the space provided in this booklet.
There is space at the back for answers which overflow the allotted space.
Surname
Forenames
Preferred Name
(if different to forenames)
Student ID
Username
Q1
(/12)
Q4
(/16)
Q7
(/10)
Q2
(/20)
Q5
(/12)
TOTAL
Q3
(/18)
Q6
(/12)
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 2 of 14
Question 1 (12 marks)
a) In the docstring of the program below, add a short description (15 words or less) of
what the program does.
"""
"""
def process_digits(number):
digits = str(number)
result = -1
for digit in digits:
if int(digit) > result:
result = int(digit)
return result
def main():
print(process_digits(234))
main()
(6 marks)
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 3 of 14
b) Rewrite the following function using descriptive variable and function names.
def who_knows(something):
for xxxx in range(len(something) - 1, -1, -1):
a_thing = something[xxxx]
if a_thing < 0:
something.pop(xxxx)
def ( ):
(6 marks)
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 4 of 14
Question 2 (20 marks)
a) Give the output produced when the following program is executed.
def main():
print("A", end = " ")
do1()
def do1():
do3()
print("B", end = " ")
do2()
def do2():
print("C", end = " ")
def do3():
do2()
print("D", end = " ")
main()
(6 marks)
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 5 of 14
b) Using the code trace technique taught in lectures, perform a code trace on the following
program and show the output.
def first(number):
total = 6
number = second(number + total)
print("1.", number)
return number % 3
def second(value):
print("2.", value)
if value % 2 == 0:
value = value + 3
else:
value = value + 2
return value
def main():
num = 5
result = first(num)
print("3.", result)
result = second(result) + num
print("4.", result)
main()
(14 marks)
The output:
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 6 of 14
Question 3 (18 marks)
a) Complete the output produced when the following main() function is executed.
def main():
a_list = [1, 4, 3]
do_something1(a_list)
print("a_list:", a_list)
def do_something1(list1):
list2 = list1
extras = [2, 1, 4]
for element in extras:
list2.append(element)
a_list:
(6 marks)
b) Complete the output produced when the following main() function is executed.
def main():
a_list = [3, 7]
do_something2(a_list)
print("a_list:", a_list)
def do_something2(list1):
list2 = [4, 3]
for element in [2, 5]:
list1.append(element)
list1 = list2
a_list:
(6 marks)
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 7 of 14
c) Given the following code, what is the type of the three Python objects: object1,
object2 and object3?
a_list = [1, '457', 4, 'True']
a_dict = {"strangely": 2, "happy": 4}
object1 = a_list[2] / 2
object2 = [a_list.pop(2) == a_dict["happy"]]
object3 = len(a_list[1] * 3) * a_dict["strangely"]
object1:
object2:
object3:
(6 marks)
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 8 of 14
Question 4 (16 marks)
a) What are the contents of the file “Output.txt” after the following program is run?
def main():
data_dict = {850:["Kim", "Lucy"], 700:["Ken", "Mele"],
450:["Ronald"],1000:["Gill", "Bart"],
200:["Alfonso"]}
filename = "Output.txt"
write_data(filename, data_dict)
def write_data(filename, data_dict):
key_list = list(data_dict.keys())
key_list.sort()
key_list.reverse()
output_stream = open(filename, "w")
for key in key_list:
values = data_dict[key]
values.sort()
for value in values:
output_stream.write(value + " - " + str(key)
+ "\n")
output_stream.close()
main()
(9 marks)
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 9 of 14
b) Give the output produced when the following main() function is executed. Show
all your working.
def main():
number = 0
for i in range(5):
number += 1
for j in range(i):
number += 1
print(number)
(7 marks)
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 10 of 14
Question 5 (12 marks)
a) Consider the function below named get_code()that takes a string as a parameter and
returns a code consisting of 3 unique characters chosen from the parameter at random. The
same character does not appear in the code more than once. The parameter string has more
than 3 characters and contains no repeated letters.
For example, the following statement:
print(get_code('ABCDE'))
could possibly produce:
DEA
The variable names in this function have not been chosen using good style considerations.
Rewrite the function in the answer box below using descriptive variable names that
conform to the style guidelines outlined in lectures and labs:
def get_code(w):
a = ''
for i in range(3):
p = random.randrange(0, len(w))
a += w[p]
w = w[0:p] + w[p+1:]
return a
def get_code( ):
(6 marks)
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 11 of 14
b) Consider the function below named swap_halves(numbers)which takes a list of
integers as a parameter and returns a new list that has every element in the second half of the
original list swapped with every element in the first half of the original list.
def swap_halves(numbers):
mid = len(numbers)//2
return numbers[mid:] + numbers[0:mid]
In the box below, write two doctests for the swap_halves() function - one that fails
the test and one that passes the test.
def swap_halves(numbers):
"""
"""
(6 marks)
mid = len(numbers)//2
return numbers[mid:] + numbers[0:mid]
import doctest
doctest.testmod()
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 12 of 14
Question 6 (12 marks)
a) Assume that the variable, value, has been initialised to some integer value. Write a
boolean expression which tests if value is exactly between 2 and 12 (both inclusive).
(3 marks)
b) Assume that the variable, value, has been initialised to some integer value. Write a
boolean expression which tests if value is exactly divisible by 13 but not divisible by 5.
(3 marks)
c) Assume that the string variable, city, has been initialised to some value. Write a
boolean expression which tests if city is either “Gore” or “Auckland” or “Dunedin”.
(3 marks)
d) Assume that the string variable, word, has been initialised to some value. Write a
boolean expression which tests if word contains the letter “b” and does not contain the
letter “d”.
(3 marks)
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 13 of 14
Question 7 (10 marks)
As accurately as possible, in the window below, show what is drawn when the following
program is executed. The grid lines have been drawn in the window to help you. The gap
between adjacent gridlines is 10 pixels.
def draw_pattern(a_canvas, left, top, size):
number_of_shapes = 3
for count in range(number_of_shapes):
rect = (left, top, left + size, top + size)
a_canvas.create_rectangle(rect)
left = left + size
top = top + size
size = size + 10
def main():
...
draw_pattern(a_canvas, 10, 10, 10)
window.mainloop()
main()
(10 marks)
10 20 30 40 50 60 70 80 90 100 110 120
10
20
30
40
50
60
70
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 14 of 14
OVERFLOW PAGE
(If you have used this page, please indicate clearly under the
relevant question that you have overflowed to this page)