The University of Auckland: First Semester, 2016 Campus: City
The University of Auckland: First Semester, 2016 Campus: City
COMPUTER SCIENCE
Principles of Programming
Note:
• The use of calculators is NOT permitted.
• You should separate the Section A Question Booklet from the Section B Question/Answer
Booklet. You may keep the Section A Question Booklet. You must hand in the Section B
Question/Answer booklet and the Teleform sheet.
• Compare the exam version number on the Teleform sheet supplied with the version number
above. If they do not match, ask the supervisor for a new sheet.
• Enter your name and Student ID on the Teleform sheet. Your name and Student ID should be
entered left aligned. If your name is longer than the number of boxes provided, truncate it.
• Answer Section A on the Teleform answer sheet provided. For Section A, use a dark pencil to
mark your answers in the answer boxes on the Teleform sheet. Check that the question
number on the sheet corresponds to the question number in this question/answer book. Do not
cross out answers on the Teleform sheet if you change your mind. You must completely erase
one answer before you choose another one. If you spoil your sheet, ask the supervisor for a
replacement. There is one correct answer per question.
• Answer Section B in the space provided in the Section B Question/Answer Booklet.
• Attempt all questions. Write as clearly as possible. The space provided will generally be
sufficient but is not necessarily an indication of the expected length. Extra space is provided at
the end of this exam book.
Page 1 of 21
VERSION 00000001 COMPSCI101
SECTION A
Question 1
[2.5 marks] Which of the following could NOT be produced by the code below?
import random
var1 = random.randrange(19, 41 ,4)
var2 = random.randrange(17, 45, 5)
var3 = random.randrange(42, 18, -3)
print(max(var1, var2, var3))
(a) 21
(b) 17
(c) 39
(d) 32
(e) 42
Question 2
[2.5 marks] What is the output produced by the following code?
value = 3 ** (1 + 1 * 2) // 5 % (20 / 2)
print(value)
(a) 10.0
(b) 6
(c) 10
(d) 5.0
(e) 5
Page 2 of 21
VERSION 00000001 COMPSCI101
Question 3
[2.5 marks] What is the output produced by the following code if the user enters “nothing is
impossible” at the prompt?
Question 4
[2.5 marks] What is the output produced by the following code?
def main():
value1 = 20
value2 = 40
print(function1(value1, value2), value2)
main()
(a) 20 40
(b) 40 20
(c) 40 40
(d) 20 20
(e) None of the above.
Page 3 of 21
VERSION 00000001 COMPSCI101
Question 5
[2.5 marks] What is the output of the following code?
def main():
function3(2, 4, 6)
main()
(a) IV
(b) II
(c) III
(d) I
(e) None of the above.
Question 6
[2.5 marks] What is the output of the following code?
def function5():
total = 0
for number in range(1, 20):
if number % 2 == 0 and number % 3 == 0:
total += number
print(number, end=" ")
print(total)
def main():
function5()
main()
(a) 2 3 4 6 8 9 10 12 14 15 16 18 117
(b) 5 10 15 30
(c) 2 3 6 8 12 20 51
(d) 6 12 18 36
(e) None of the above.
Page 4 of 21
VERSION 00000001 COMPSCI101
Question 7
[2.5 marks] What is the output produced by the following code?
list1 = [3, 2, 4]
list1.append(7)
list1.append(1)
list1.insert(3, 5)
num1 = list1.pop(0)
num2 = list1.pop()
if 5 in list1:
num3 = list1.index(5)
list1.pop(num3)
print(list1)
(a) [2, 4, 5, 4]
(b) [2, 4, 4, 7]
(c) [2, 4, 5, 4, 7, 1]
(d) [2, 4, 5, 4, 7]
(e) [2, 4, 4, 7, 1]
Question 8
[2.5 marks] What is the output produced by the following code?
list1 = [1, 2, 3, 4, 5, 6]
print(list2)
(a) [1, 2, 4, 6, 2, 3, 4, 5]
(b) [1, 2, 4, 6, 2, 3, 4, 4]
(c) [1, 2, 4, 5, 6, 2, 3, 5]
(d) [1, 2, 4, 6, 2, 3, 5]
(e) [1, 2, 4, 6, 3, 2, 5]
Page 5 of 21
VERSION 00000001 COMPSCI101
Question 9
[2.5 marks] What is the output produced by the following program?
def main():
list1 = [1, 6, 3, 4, 9, 12]
process_list(list1, 3)
print(list1)
main()
Page 6 of 21
VERSION 00000001 COMPSCI101
Question 10
[2.5 marks] What is the output produced by the following program?
def main():
a_list1 = [5, 3, 2]
a_list2 = [4, 6]
fiddle_lists(a_list1, a_list2)
print("a_list1:", a_list1)
print("a_list2:", a_list2)
main()
Question 11
[2.5 marks] Consider a window that is 400 pixels wide and 200 pixels high, as shown below:
Which of the following method calls would correctly draw a circle that is positioned exactly in the
centre of the window, and has a diameter of 100 pixels?
Page 7 of 21
VERSION 00000001 COMPSCI101
Question 12
[2.5 marks] Consider the following function:
(a) 250
(b) 110
(c) 400
(d) 2
(e) None of the above.
Question 13
[2.5 marks] Suppose the following code fragment:
def main():
countries = {'usa':("english","Washington D.C."), \
'poland':("polish","Warsaw")}
#complete this
main()
produces
Which of the following lines of code, if added to the main() function, would produce the above
output?
Page 8 of 21
VERSION 00000001 COMPSCI101
Question 14
[2.5 marks] What is the output of the following code fragment?
value = 0
for i in range(0, 25, 10):
print(value, end=" ")
for j in range(0, 10, 5):
value += 1
print('final value', value)
Page 9 of 21
VERSION 00000001 COMPSCI101
Question/Answer Sheet ID ……….…………
Page 10 of 21
VERSION 00000001 COMPSCI101
Computer Science
Principles of Programming
Answer all questions in this section in the space provided. If you run out of space then please use the
Overflow Sheet and indicate in the allotted space that you have used the Overflow Sheet.
Surname:
First Name(s):
Student ID:
MARKERS ONLY
(/35) (/13)
Q15 Q18
(/13) (/13)
Q16 Q19
Page 11 of 21
VERSION 00000001 COMPSCI101
a) Complete the make_banner() function below which takes a single string parameter that
provides the information needed to print a banner. The string is formatted as follows:
• the last character of the string is used for the banner frame.
• the second to last character of the string determines how wide the banner frame is
on either side of the banner text. You can assume this will be a character between
0 and 9.
• the remainder of the string is the banner text. You can assume that the banner text
will be at least one character long.
For example, when the following program is executed with the completed function, the output
is:
######################
###COMPSCI 101 Exam###
######################
def main():
make_banner("COMPSCI 101 Exam3#")
def make_banner(information):
frame_char = information[-1]
frame_width = int(information[-2])
banner_text = information[:-2]
print(frame_char * (len(banner_text) + 2 * \
frame_width))
print(frame_char * (len(banner_text) + 2 * \
frame_width))
(6 marks)
main()
Page 12 of 21
VERSION 00000001 COMPSCI101
b) Rewrite the function3() function shown below, using a for … in … range() loop.
Both functions should behave in exactly the same way.
def function3(num):
div = 1
result = ""
while div <= num:
if num % div == 0:
result = result + " " + str(div)
div = div + 1
return result
def function3(num):
(7 marks)
result = ""
for div in range(1,num+1):
if num % div == 0:
result = result + " " + str(div)
return result
Page 13 of 21
VERSION 00000001 COMPSCI101
a) Complete the get_cheapest() function below which takes two lists as parameters:
a list of strings, where each string is a description of an item,
and,
a list of floats where the values represent the price of each item in the first parameter list.
The function returns a string containing information about the cheapest item in the list. The
string returned by the function is made up of the item description followed by a ' $' sign and the
price rounded to the nearest dollar. If two items have the same price, the cheapest is the last
item from the beginning of the list. You can assume both lists have the same length and that
they are not empty lists.
For example, when the program is executed with the completed function, the output is:
1. ['hat', 'shoes', 'bag', 'gloves', 'scarf']
2. [59.5, 275.75, 195.75, 65.15, 85.75]
3. hat $60
def main():
items = ["hat", "shoes", "bag", "gloves", "scarf"]
prices = [59.5, 275.75, 195.75, 65.15, 85.75]
print("1.", items)
print("2.", prices)
print("3.", info_cheapest)
index_of_cheapest = 0
for i in range(1,len(prices)):
if prices[i] <=
prices[index_of_cheapest]:
index_of_cheapest = i
(7 marks)
main()
Page 14 of 21
VERSION 00000001 COMPSCI101
b) Complete the get_big_three() function below which takes two tuples containing integer
values as parameters and returns a tuple containing the three largest integer values from the
combined values of the two parameter tuples. The numbers in the tuple returned by the function
should be sorted from smallest to largest. You can assume that the two parameter tuples contain
three or more elements when combined.
For example, when the program below is executed with the completed function, the output is:
1. (2, 9, 8, 1)
2. (5, 11, 4)
3. (8, 9, 11)
def main():
t1 = (2, 9, 8, 1)
t2 = (5, 11, 4)
t3 = get_big_three(t1, t2)
print("1.", t1)
print("2.", t2)
print("3.", t3)
(6 marks)
main()
Page 15 of 21
VERSION 00000001 COMPSCI101
The following program reads information from the input file, 'Groceries.txt', and writes parts
of the information to the output file, 'Shopping.txt'.
a) Complete the read_grocery_list() function which takes a file name as a parameter and
returns a list of lines read from the file. Note that none of the string elements of the returned list
should contain a newline character ("\n").
b) Complete the write_numbered_list() function which takes a file name and a list of strings
as parameters. The first line written to the file is "Shopping List", followed by a blank line.
Each element of the parameter list is a string containing three comma separated bits of
information describing a grocery item, e.g., 'BC37882,Noodles,3.95', where the first part
of the string is the item code, the second part is the item name and the last part is the item cost.
For each element of the parameter list of strings, the function writes a number (starting from 1),
followed by a full stop and a space, followed by the item name, followed by a space and the dollar
sign, and lastly the cost of the item. Each string is written on a new line.
An example of a 'Groceries.txt' file (on the left) and the corresponding 'Shopping.txt'
file (on the right) produced by the completed program are shown below:
def main():
grocery_list = read_grocery_list("Groceries.txt")
write_numbered_list("Shopping.txt", grocery_list)
def read_grocery_list(filename):
input_stream = open(filename,"r")
contents = input_stream.read()
input_stream.close()
content_list = contents.split("\n")
return content_list
Page 16 of 21
VERSION 00000001 COMPSCI101
(5 marks)
(8 marks)
main()
Page 17 of 21
VERSION 00000001 COMPSCI101
The function returns a list containing the index positions of the target word in the string of
words. For example, the following code:
prints
[3, 9]
because ‘equal’ appears at position 3, and position 9 in the string. (We start counting positions
of words in the string from 0.)
results = []
str_list = the_string.split()
for i in range(len(str_list)):
if str_list[i] == target:
results.append(i)
return results
(6 marks)
Page 18 of 21
VERSION 00000001 COMPSCI101
may print:
Note: you MUST call the get_indexes() function defined in Part a) to solve this
problem.
def build_index_dict(the_string):
str_dict = {}
str_list = the_string.split()
return str_dict
(7 marks)
Page 19 of 21
VERSION 00000001 COMPSCI101
for j in range(number_of_columns):
rect = (x_left, y_down, x_left + size, y_down + size)
a_canvas.create_rectangle(rect, fill='blue')
x_left += dist #Position A
y_down += size #Position B
def main():
root = Tk()
root.title("A Canvas")
root.geometry("400x300+10+20")
a_canvas = Canvas(root)
a_canvas.pack(fill = BOTH, expand = True)
rectangular_grid(a_canvas)
root.mainloop()
main()
a) In total, how many times is the statement marked Position A in the program above executed
when the program is run?
15
(2 marks)
b) In total, how many times is the statement marked Position B in the program above executed
when the program is run?
(2 marks)
Page 20 of 21
VERSION 00000001 COMPSCI101
c) 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.
(9 marks)
Page 21 of 21