The University of Auckland: Total
The University of Auckland: Total
The University of Auckland: Total
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
Student ID
Username
Q1 Q4 Q7
Page 1 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
Question 1 (15 marks)
a) Complete the output produced by the following code.
result = 2 + 3 ** 2 * 2 - 3 // 2
print("Result:", result)
Result:
(3 marks)
place = "HOKITIKA"
place = place[4:] + place[:2] + place[-3]
print("place:", place)
place:
(3 marks)
c) Complete the output produced by the following code.
return result
def main():
print("1.", get_convert_num(101, "stuvwxyzab"))
print("2.", get_convert_num(251, "abcdefghij"))
main()
1.
2.
(4 marks)
Page 2 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
def main():
print("1.", add_slice("glad", 3, True))
print("2.", add_slice("glad", 2, False))
print("3.", add_slice("short", 3, False))
print("4.", add_slice("life", 2, True))
(5 marks)
Page 3 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
Question 2 (15 marks)
a) 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.
(3 marks)
(3 marks)
c) The following program contains five completed functions and an incomplete main()
function. The completed program does the following:
• gets two random numbers,
• generates a string containing the sum of the two random numbers,
• asks the user for the answer to this sum,
• checks the user's answer,
and
• gives appropriate feedback based on the user's answer.
Below are three example outputs using the completed program (the user input is shown
in a larger font):
5 + 8 = 10
9 + 2 = 11 The correct answer: 5 + 8 = 13
Excellent!
4 + 3 = 5
The correct answer: 4 + 3 = 7
Page 4 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
Complete the main() function below by writing FOUR statements where each
statement contains a call to one of the five functions. Your completed program should
behave in the manner described above.
import random
def get_random_number():
return random.randrange(2, 10)
def get_user_answer(prompt):
user_input = input(prompt)
return int(user_input)
def main():
num1 = get_random_number()
num2 = get_random_number()
(9 marks)
main()
Page 5 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
Question 3 (15 marks)
a) Give the output produced by the following code.
total = 10
for counter in range(0, 12, 4):
if total > 6:
total = total - counter
else:
total = total + counter
print(counter, total)
print("total:", total)
(3 marks)
b) Rewrite the following code using an equivalent while loop instead of the for ...
in range() loop:
total = 10
print("End")
(3 marks)
Page 6 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
import random
def main():
print("1.", get_random_digits(5))
print("2.", get_random_digits(3))
print("3.", get_random_digits(10))
def get_random_digits(number_of_digits):
(3 marks)
main()
Page 7 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
def main():
list1 = [1, 2, 3]
list2 = [4, 5, 6]
muddle1(list1, list2)
print("list1:", list1)
print("list2:", list2)
main()
list1:
list2:
(3 marks)
Page 8 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
def main():
tuple1 = (1, 2)
tuple2 = (3, 4)
tuple1 = muddle2(tuple1, tuple2)
print("tuple1:", tuple1)
print("tuple2:", tuple2)
main()
tuple1:
tuple2:
(3 marks)
Page 9 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
Question 4 (15 marks)
a) In the boxes below, show each element of a_list after the following code has been
executed. Use as many of the boxes as you need.
a_list = [5, 2, 4, 3, 1]
a_list.insert(3, 4)
value = a_list.pop()
a_list.insert(2, value)
value = a_list.pop(0)
value = value * a_list.index(4)
a_list.append(value)
(3 marks)
a_list = [2, 6, 1]
for index in range(len(a_list)):
position = a_list[index]
if position < len(a_list):
a_list[position] = a_list[position] + 1
print(index, a_list)
(3 marks)
Page 10 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
c) Given the following code, what is the type of each of the three Python objects
(object1, object2 and object3)?
a_list = [True, 4, "7", '56']
object1 = a_list[2] * 3
object2 = a_list[0:1]
object3 = a_list[3].rfind("4")
object1 is of type:
object2 is of type:
object3 is of type:
(3 marks)
NOTE: you can assume that all the elements in the list contain two or more letters.
def main():
names = ["karl", "Orlando", "carlo", "zAC"]
print("1. names:", names)
convert_first_letter(names)
print("2. names:", names)
def convert_first_letter(names_list):
(6 marks)
main()
Page 11 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
Question 5 (15 marks)
The following program reads information from the input file, "A2Marks.txt", and
writes a summary of the information to the output file, "A2Stats.txt".
• The input file contains a series of marks out of 100. The marks are all whole numbers
and are separated by a blank space.
• The output file displays four lines of text summarising the assignment marks.
Below is an example of an "A2Marks.txt" file (on the left) and the corresponding
"A2Stats.txt" file (on the right) produced by the completed program:
def main():
list_of_marks = get_list_of_marks("A2Marks.txt")
marks_stats_tuple = get_marks_stats_tuple(2, list_of_marks)
write_stats_to_file("A2Stats.txt", marks_stats_tuple)
def get_list_of_marks(filename):
Page 12 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
main()
(15 marks)
Page 13 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
Question 6 (15 marks)
a) Complete the following code which adds 1 to all the values corresponding to the keys in
the a_dict dictionary. For example, the output of the completed code is:
print("2.", a_dict)
(4 marks)
b) Complete the output produced when the following main() function is executed:
def main():
words = ["exceptional", "big", "delectable", "and", "peculiar"]
phrase = ""
for a_word in words:
phrase = phrase + get_a_word(a_word, 2) + " "
print("phrase:", phrase.strip())
if word in a_dict:
alternatives = a_dict[word]
return alternatives[index]
return word
Page 14 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
phrase:
(4 marks)
• the keys are strings made up of the first letter concatenated with the last letter of a
word,
• the corresponding values are a list of all the words where the first letter concatenated
with the last letter is the key.
NOTE: any word in the list which has fewer than two characters is ignored.
Executing the following program with the completed function, prints:
def main():
list_of_words = ["may", "a", "many", "soft", "seat", "love"]
a_dict = get_first_last_dict(list_of_words)
print("dict:", a_dict)
def get_first_last_dict(word_list):
(7 marks)
main()
Page 15 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
Question 7 (10 marks)
def draw_pattern(a_canvas):
symbols_line1 = "1S2O1"
symbols_line2 = "2O1S2"
size = 10
start_left = size
down = size
number_to_do = 3
current_line = symbols_line1
def main():
root = Tk()
root.title("A Canvas")
root.geometry("455x255+10+10")
a_canvas = Canvas(root, bg="white")
a_canvas.pack(fill=BOTH, expand=1) #Canvas fills whole window
draw_pattern(a_canvas)
root.mainloop()
main()
Page 16 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
(2 marks)
b) As accurately as possible, in the window below, show what is drawn by the above
program. Grid lines have been drawn in the window to help you. The gap between
adjacent gridlines is 10 pixels.
10 20 30 40 50 60 70 80 90 100 110
10
20
30
40
50
60
70
80
90
100
(8 marks)
Page 17 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
OVERFLOW PAGE
(If you have used this page, please indicate clearly under the
relevant question that you have overflowed to this page)
Page 18 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
Page 19 of 20
Question/Answer Booklet COMPSCI 101
ID: ………………
________________________________________
Page 20 of 20